字典长度已知,直接开个结构体数组存单词,顺便保存单词长度,搜索的时候可以迅速检测不需要的单词,然后开一个30的数组保存每个单词出现的个数
最后要对字典排序(当时没看题忘排序了,WA了一次)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct words
{
char word[101];
int length;
int check[30];
}dictionary[100000];
int cmp(void const* a,void const *b)
{
struct words A=*(struct words *)a;
struct words B=*(struct words *)b;
return strcmp(A.word,B.word);
}
int checkal(int s1[],int s2[])
{
int i=0;
for(i=0;i<30;++i)
{
if(s1[i]!=s2[i])
{
return 0;
}
}
return 1;
}
int main()
{
int i=0,j=0,count=0;
char data[101]={0};
int check[30]={0};
char flag=0;
while(scanf("%s",dictionary[i].word)!=EOF&&dictionary[i].word[0]!='X')
{
for(j=0;j<30;j++)
{
dictionary[i].check[j]=0;
}
for(j=0;dictionary[i].word[j];++j)
{
++dictionary[i].check[dictionary[i].word[j]-'a'];
}
dictionary[i++].length=j;
}
qsort(dictionary,i,sizeof(struct words),cmp);
count=i;
while(scanf("%s",data)&&data[0]!='X')
{
flag=0;
for(i=0;i<30;++i)
{
check[i]=0;
}
for(i=0;data[i];++i)
{
++check[data[i]-'a'];
}
for(j=0;j<count;++j)
{
if(dictionary[j].length==i&&checkal(dictionary[j].check,check))
{
flag=1;
puts(dictionary[j].word);
}
}
if(!flag)
{
puts("NOT A VALID WORD");
}
puts("******");
}
}