Question:
Ask if the string you entered can be changed to a dictionary string by adding or subtracting one character or replacing one character.
Train of Thought Analysis:
Directly simulate the process of replacing, adding, and subtracting.
Compare the length of two strings. Simulation can be performed only when the difference is 1.
The simulation process is to perform matching one by one.
If the mismatch is found to be less than or equal to 1, the output can be made.
#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;}