题目大意:给定一个字典,然后给定一组单词集合,如果该单词在字典中,则输出correct;否则,如果该单词能够通过1>插入一个字母 2>删除一个字母 3>替换一个字母三种操作中的一种转化为字典序中的某个单词,输出该单词。直接暴力过的,c++能过,g++超时。
//11222230 c00h00g 1035 Accepted 1200K 500MS C++ 1530B 2013-01-30 15:07:29
#include<string>
#include<iostream>
#include<map>
using namespace std;
string word[10005];
map<string,int> mp;
//判断单词能否替换
bool check_replace(string a,string b,int len){
int res=0;
for(int i=0;i<len;i++)
if(a[i]!=b[i]) res++;
if(res==1) return true;
return false;
}
//判断能否通过插入或者删除,如果插入,将长度小的字符串写在前面
bool check_insert(string a,string b,int len1,int len2){
int i=0,j=0;
while(i<len1&&j<len2&&a[i]==b[j]){i++;j++;}
j++;//b向后移动一个位置
while(i<len1&&j<len2&&a[i]==b[j]){i++;j++;}
if(i==len1&&j==len2) return true;
return false;
}
int main(){
int i=0;
while(cin>>word[i]&&word[i]!="#"){
mp[word[i]]=1;
i++;
}
int n=i;
string test;
while(cin>>test&&test!="#"){
if(mp[test])
cout<<test<<" is correct"<<endl;
else{
cout<<test<<":";
int len1=test.length();
for(int i=0;i<n;i++){
int len2=word[i].length();
if(len1==len2){
if(check_replace(test,word[i],len1))
cout<<" "<<word[i];
}else if(len1+1==len2){//insert a char
if(check_insert(test,word[i],len1,len2))
cout<<" "<<word[i];
}else if(len1-1==len2){//delete a char
if(check_insert(word[i],test,len2,len1))
cout<<" "<<word[i];
}
}
cout<<endl;
}
}
return 0;
}