原题链接:Spell checker
题意:
?deleting of one letter from the word; 删除
?replacing of one letter in the word with an arbitrary letter; 替换
?inserting of one arbitrary letter into the word. 插入
输入一个多个单词组成的字典(以#结束),再输入一些单词(以#结束),若这个单词在字典里能找到就输出:字符串 is correct
若这个单词删除、替换、插入一个字符后能在字典中找到就输出:字符串: .... (样例中都有,要注意的一点就是要按字典中单词的顺序输出)
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
string str[10000 + 10];
void calc(string s, int len){
int i, j;
for(i = 0;i < len;i ++){ //能找的的情况
if(str[i] == s){
cout << s << " is correct" << endl;
return ;
}
}
cout << s << ":";
for(i = 0;i < len;i ++){ //替换、删除、添加
int len1 = str[i].length(), len2 = s.length();
if(len1 == len2){ //替换
for(j = 0;j < len1 && str[i][j] == s[j];j ++); //长度相等,有不同的字符时替换
for(++j;j < len1 && str[i][j] == s[j];j ++);
if(j == len1) cout << " " << str[i]; //替换之后能比较到len1
}else if(len1 == len2 - 1){ //删除
for(j = 0;j < len1 && str[i][j] == s[j];j ++);
for(;j < len1 && str[i][j] == s[j + 1];j ++);
if(j == len1) cout << " " << str[i];
}else if(len1 == len2 + 1){ //添加
for(j = 0;j < len2 && str[i][j] == s[j];j ++);
for(;j < len2 && str[i][j + 1] == s[j];j ++);
if(j == len2) cout << " " << str[i];
}
}
cout << endl;
}
int main(){
int i = -1, j;
string s;
while(cin >> str[++ i] && str[i] != "#");
while(cin >> s && s != "#"){
calc(s, i); //字符串,字典的大小
}
return 0;
}