找差异为一个字符的串
http://poj.org/problem?id=1035
#include<iostream> #include<string> #include<string.h> #include<stdlib.h> #include<stdio.h> #define N 10005 using namespace std; string str[N]; int num,l,b[N]; int replace(string c,string b) { int i,sum=0; for(i=0;i<c.length();i++) { if(c[i]!=b[i]) { sum++; if(sum>1)return 0; } } return 1; } int insert(string c,string b) { int i,sum=0; int k=0; for(i=0;i<c.length();i++)//注意这里c是长度较长的 { if(c[i]!=b[k]) { sum++; k--; if(sum>1)return 0; } k++; } return 1; } int main() { string c; num=0; int i,j; while(cin>>str[num]) { if(str[num]=="#"){break;} num++; } getchar(); while(cin>>c) { if(c=="#")break; int flag=0; for(i=0;i<num;i++) { if(str[i]==c) { cout<<str[i]<<" is correct"<<endl; flag=1; break; } } if(flag)continue; cout<<c<<":"; for(i=0;i<num;i++) { if(str[i].length()==c.length()) { if(replace(str[i],c)) { cout<<" "<<str[i]; } } if(str[i].length()==c.length()+1) { if(insert(str[i],c))cout<<" "<<str[i]; } if(str[i].length()+1==c.length()) { if(insert(c,str[i]))cout<<" "<<str[i]; } } cout<<"\n"; } }