you do not need to use complicated algorithms, the final return value for your 20万到50万个单词 will be "abcdefghijklmnopqrstuvwxyz", :-)just go through every element in your array, go through every character for each element, int[] alist = new int[26];
for (int i=0; i < alist.Length; i++)
alist[i] = 0;int a = (int)'a';int nCount = 0, w;for (int i=0; i < yourArray.Length; i++)
{
for (int j=0; j < yourArray[i].Length; j++)
{
w = (int)yourArray[i][j];
if (alist[ w- a] ==0)
alist[w - a] = ++nCount; }
}then sort alist excluding those elements with 0 value
for (int i=0; i < alist.Length; i++)
alist[i] = 0;int a = (int)'a';int nCount = 0, w;for (int i=0; i < yourArray.Length; i++)
{
for (int j=0; j < yourArray[i].Length; j++)
{
w = (int)yourArray[i][j];
if (alist[ w- a] ==0)
alist[w - a] = ++nCount; }
}then sort alist excluding those elements with 0 value
char[] clist = new char[26];int i;
for (i=0; i < alist.Length; i++)
alist[i] = 0;int a = (int)'a';
int nCount = 0, w, j;for (i=0; i < yourArray.Length; i++)
{
for (j=0; j < yourArray[i].Length; j++)
{
w = (int)yourArray[i][j];
if (alist[ w- a] ==0)
{
alist[w - a] = 1;
clist[nCount++] = yourArray[i][j];
}
}
}System.Console.WriteLine(new String(clist,0,nCount));
{
for (int j=0; j < yourArray[i].Length; j++)
{
ht.Add(yourArray[i][j],1);
}
}IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
while ( myEnumerator.MoveNext() )
System.Console.WriteLine(myEnumerator.Key);
ht.Add(yourArray[i][j],1);
===>
ht[yourArray[i][j]] = 1;or if you want word count, you can do something likeif (ht.Contains(yourArray[i][j]))
ht[yourArray[i][j]] = (int)ht[yourArray[i][j]] + 1;
else
ht[yourArray[i][j]] = 1;
先按照第一个字母大小排序,如果相同,则按照单词长度排序,如果两者都相同,则去掉第一个字母后,再用相同的规则排序,一直到最后。取我单词列表中的一段:
ad
ah
al
am
an
ar
as
at
au
ax
abc
abe
ace
act
add
ado
aft
还有,高速的查找都是基于索引的,我觉的要分两步,稍后会给出我的算法。
---------------------------------------------
To teach a fish how to swim
[email protected]
---------------------------------------------
To teach a fish how to swim.
”如何能够较快的把里面的字母全部提取出来。要求字母不重复“假定你的字母集合基数为n 最多有n个字母是不是 !不对就不看下面并修改你的问题:D1。建立一个数组Result[n]
2。设变量I = 1 ;3。检查所有长度为I的单词(你自己说有规律是不是?)象箱排序一样把你找到的字母扔到对应的Result[x]中(一个箱中只放一个字母)4。Result数组堆满 || 没有长为 I+1的单词 Result数组中所有元素为结果
5,I++ 到 3
先决条件:
1)只有26个字符,全部是小写。
2)单词词组不太变动,而查找的算法经常用。我们可以哦那个一个32位的整数来表示“abcdefghigklmnopqrstuvwxyz"的出现情况,例如”00000000 000000 00000000 00001111“表示abcd出现过,其他的字符没有出现。” 00000011 11111111 11111111 11111111“ 表示所有的字符都出现了。基于以上原理,我们建立索引, 例如字符串"ab"转化为3,”aab"也是3。这样每一个字符串都有一个整数与之对应。当然建立索引的算法是顺序遍历,注意使用移位操作符。当有新词进来时,更新索引。查找的算法这样,从建立索引的两头向中间移动,例如第一单词是a,加入最后一个是z, 则把a对应的数字索引 "00000000 00000000 00000001"和z对应的"00000010 00000000 00000000 00000000"实行按位与操作,得到“00000010 00000000 00000000 00000001”,一直这样累计下去。中间要把累计的结果与“00000011 11111111 11111111 11111111”执行异或操作,执行结果与0比较,等于0表示26个字符都有了,用break跳出循环。当然,你要是在建立索引的时候就把那个累计结果给求出来,哈哈,查找就一步。最快。这个算法是用整数操作代替字符操作,全是移位操作,一个指令循环可以作完一个移位。内存占有比原来少。当然,这个游戏上没有什么实际价值,大家玩玩罢了。
-----------------------------------------------------
To teach a fish how to swim