標籤:os io for re c ar
題目大意:
問你後面輸入的串能不能通過 加減一個字元,或者替換一個字元變成字典中的串。
思路分析:
直接類比替換加減的過程。
比較兩個串的長度。要相差為1 的時候才能進行類比。
類比的過程就是進行一個個的匹配。
發現失配的次數小於等於 1就可以輸出。
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <string>#include <map>#define maxn 10005using namespace std;char str[maxn][20];string tmp;map <string,bool>mymap;int main(){ while(scanf("%s",str[0])!=EOF) { mymap.clear(); tmp=str[0]; mymap[tmp]=true; int i=1; while(scanf("%s",str[i]) && str[i][0]!='#') { tmp=str[i]; mymap[tmp]=true; i++; } char txt[20]; while(scanf("%s",txt) && txt[0]!='#') { tmp=txt; if(mymap[tmp])printf("%s is correct\n",txt); else { printf("%s:",txt); int l=strlen(txt); for(int j=0;j<i;j++) { int len=strlen(str[j]); if(l+1==len) { int cnt=0; int p=0,k=0; while(p<l && k<len) { if(txt[p]==str[j][k]) p++,k++; else { cnt++; k++; } } if(cnt<=1 && p==l)printf(" %s",str[j]); } if(l==1+len) { int cnt=0; int p=0,k=0; while(p<len && k<l) { if(str[j][p]==txt[k])p++,k++; else { cnt++; k++; } } if(cnt<=1 && p==len)printf(" %s",str[j]); } if(l==len) { int cnt=0; for(int k=0;k<len;k++)if(txt[k]!=str[j][k])cnt++; if(cnt==1)printf(" %s",str[j]); } } puts(""); } } } return 0;}