/*用暴力搜索即可解决,用trie数来解决*/
#include <iostream>
#include <string>
#include <string.h>
#define branchNum 26
using namespace std;
typedef struct Trie_Node
{
int index;
int len;
Trie_Node *next[branchNum];
Trie_Node():index(-1),len(0)
{
memset(next,NULL,sizeof(next));
}
}Trie;
Trie *location;
int cnt=0,flag;
char resource[10002][27],res[10002][27];
void create(char *word)
{
Trie *root=location;
int len=strlen(word);
while (*word)
{
if(root->next[*word-'a']==NULL)
{
Trie *temp=new Trie();
root->next[*word-'a']=temp;
}
root=root->next[*word-'a'];
++word;
}
root->index=cnt++;
root->len=len;
}
int search(char *word)
{
Trie *root=location;
int len=strlen(word);
while(root&&*word)
{
if(root->next[*word-'a']==NULL)
return -1;
root=root->next[*word-'a'];
word++;
}
if(root->len==len)
return root->index;
else return -1;
}
void add(char *word)
{
char temp[27];
int len=strlen(word),pos,j;
//i一直要到len,这样才能保证在串的后面加上
for (int i=0;i<=len;i++)
{
for (char tempLetter='a';tempLetter<='z';tempLetter++)
{
for (j=0;j<i;++j)
{
temp[j]=word[j];
}
temp[j]=tempLetter;
for (j=i+1;j<=len;++j)
{
temp[j]=word[j-1];
}
temp[len+1]='\0';
pos=search(temp);
if(pos==-1)
continue;
strcpy(res[pos],temp);
flag=1;
}
}
}
void mydelete(char *word)
{
char temp[27];
int len=strlen(word);
for (int i=0;i<len;++i)
{
int k=0;
for (int j=0;j<len;++j)
{
if(j==i)
continue;
temp[k++]=word[j];
}
temp[k]='\0';
int pos=search(temp);
if(pos==-1)
continue;
else strcpy(res[pos],temp);
flag=1;
}
}
void DeleteTrie(Trie *root)
{
for (int i=0;i<branchNum;++i)
{
if (root->next[i]==NULL)
{
delete(root->next[i]);
}
else
DeleteTrie(root->next[i]);
}
delete(root);
}
void change(char *word)
{
int len=strlen(word);
char temp[27];
strcpy(temp,word);
for (int i=0;i<len;++i)
{
for (char tempLetter='a';tempLetter<='z';++tempLetter)
{
if(temp[i]==tempLetter)
continue;
temp[i]=tempLetter;
int pos=search(temp);
if(pos==-1) continue;
strcpy(res[pos],temp);
flag=1;
}
temp[i]=word[i];
}
}
int main()
{
int i;
//freopen("in.txt","r",stdin);
location=new Trie();
char query[27];
for(i=0;;++i)
{
cin >> resource[i];
if (resource[i][0]=='#')
break;
create(resource[i]);
}
while(1)
{
flag=0;
cin >> query;
memset(res,'\0',sizeof(res));
if(query[0]=='#')
break;
int pos=search(query);
if(pos!=-1)
cout << query << " is correct" << endl;
else
{
add(query);
mydelete(query);
change(query);
if(flag)
{
cout << query << ":";
for (int k=0;k<10001;k++)
{
if (res[k][0]!='\0')
{
cout << " "<< res[k];
}
}
cout << endl;
}
else
cout << query << ":" << endl;
}
}
DeleteTrie(location);
}
其实应该早就ac了,中间的Correct应该是小写,我弄成大写了。。。。但是貌似把一个简单的问题复杂化了。。。。