欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.NET 臟字過濾算法

 更新時(shí)間:2009年10月25日 10:15:24   作者:  
asp.NET 臟字過濾算法,需要參考上一篇文章,大家可以比較下。
原文見http://www.dbjr.com.cn/article/20575.htm
但在我這里測試的時(shí)候,RegEx要快一倍左右。但是還是不太滿意,因?yàn)槲覀兙W(wǎng)站上臟字過濾用的相當(dāng)多,對效率已經(jīng)有了一些影響,經(jīng)過一番思考后,自己做了一個(gè)算法。在自己的機(jī)器上測試了一下,使用原文中的臟字庫,0x19c的字符串長度,1000次循環(huán),文本查找耗時(shí)1933.47ms,RegEx用了1216.719ms,而我的算法只用了244.125ms.
更新:新增一個(gè)BitArray,用于判斷某char是否在所有臟字中出現(xiàn)過。總時(shí)間由244ms降到了34ms.
主要算法如代碼所示
復(fù)制代碼 代碼如下:

private static Dictionary dic = new Dictionary();
private static BitArray fastcheck = new BitArray(char.MaxValue);
static void Prepare()
{
string[] badwords = // read from file
foreach (string word in badwords)
{
if (!dic.ContainsKey(word))
{
dic.Add(word, null);
maxlength = Math.Max(maxlength, word.Length);
fastcheck[word[0]] = true;
}
}
}

使用的時(shí)候
復(fù)制代碼 代碼如下:

int index = 0;
while (index < target.Length)
{
if (!fastcheck[target[index]])
{
while (index < target.Length - 1 && !fastcheck[target[++index]]) ;
}
for (int j = 0; j < Math.Min(maxlength, target.Length - index); j++)
{
string sub = target.Substring(index, j);
if (dic.ContainsKey(sub))
{
sb.Replace(sub, "***", index, j);
index += j;
break;
}
}
index++;
}

相關(guān)文章

最新評(píng)論