//Spell checker
#include <iostream>
#include <cstdio>
#include <string>
#include <set>
using namespace std;
set<string> dic;
string dictxt[10005];
int index = 0;
bool isInDic(string& s)
{
set<string>::iterator it;
it = dic.find(s);
if (it != dic.end())
return true;
return false;
}
void search(string& str)
{
if(isInDic(str))
{
cout<<str<<" is correct"<<endl;
return;
}
cout<<str<<":";
int i,j;
int lens = str.length(),
lend;
string s;
for (i = 0; i < index; ++i)
{
lend = dictxt[i].length();
switch(lens-lend)
{
case 0://replace
s = str;
for (j = 0; j < lend; ++j)
if (dictxt[i][j] != s[j])
{
s[j] = dictxt[i][j];
break;
}
if(s == dictxt[i])
cout<<" "<<dictxt[i];
break;
case -1://insert
s = str;
for (j = 0; j < lens; ++j)
if (dictxt[i][j] != s[j])
{
s.insert(j,1,dictxt[i][j]);
break;
}
if(j == lens || s == dictxt[i])
cout<<" "<<dictxt[i];
break;
case 1://delete
s = str;
for (j = 0; j < lend; ++j)
if (dictxt[i][j] != s[j])
{
s.erase(j,1);
break;
}
if (j == lend || s == dictxt[i])
cout<<" "<<dictxt[i];
break;
}
}
cout<<endl;
}
int main()
{
freopen("in.txt","r",stdin);
index = 0;
while (cin>>dictxt[index] && dictxt[index] != "#")
{
dic.insert(dictxt[index]);
++index;
}
string s;
while (cin>>s && s != "#")
search(s);
}
测试数据:
aaa
aab
#
aaa
aa
aaaa
aba
baa
aab
bba
abb
bab
#
i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#
第一组测试数据很重要,注意边界情况的处理。