NYoj 162 spell checker
来源:互联网 发布:通赢软件下载 编辑:程序博客网 时间:2024/05/09 14:17
/* NYoj 162 Spell checker 虽然是一道简单的题,但是还是花了我一个上午的时间的调试. 汗!明显的可以看出自己的代码及调试的能力. 以后要特别注意代码和调试能力,以为在平常的练习赛中, 我发现,大部分时间都花在调试的时间了,不是没有思路. 而自己写代码,自己调试是最好的锻炼的机会.*/ #include<iostream>#include<string.h>using namespace std;char list[10002][16];int numa,numl,mark[10002];bool is_correct(char a[]){ for(int i=1;i<=numl;i++) if(strcmp(list[i],a)==0) return true; return false;}bool dele_one(char a[],char list[]){ for(int x=0;x<strlen(a);x++) { char b[20]; int top=0; for(int y=0;y<strlen(a);y++) if(y!=x) b[top++]=a[y]; if(x==0) b[top]='\0';//相当不理解. if(strcmp(b,list)==0) return true; } return false;}bool replce(char a[],char list[]){ int iSum=0; for(int x=0;x<strlen(a);x++) if(a[x]==list[x]) iSum++; //cout<<iSum<<endl; if(iSum==strlen(a)-1) return true; else return false;}bool insert(char a[],char list[]){ for(int x=0;x<strlen(list);x++) { char b[20]; int top=0; b[x]=list[x]; for(int y=0;y<strlen(a);y++) { if(top==x) top++; b[top++]=a[y]; } if(x==0) b[top]='\0';//相当不理解. if(strcmp(b,list)==0) return true; } return false;}bool judge(char a[]){ for(int i=1;i<=numl;i++) { int lena=strlen(a),lenl=strlen(list[i]); if(lena-1==lenl) { if(dele_one(a,list[i])) mark[i]=1; } else if(lena==lenl) { if(replce(a,list[i])) {mark[i]=1;} } else if(lena+1==lenl) { if(insert(a,list[i])) mark[i]=1; } } int marks=0; for(int k=1;k<=numl;k++) if(mark[k]==1) marks=1; if(marks) return true; return false;}int main(){ int i; for(i=1;;i++) { cin>>list[i]; if(strcmp(list[i],"#")==0) break; } numl=i-1; char a[52][16]; for(i=1;;i++) { cin>>a[i]; if(strcmp(a[i],"#")==0) break; } numa=i-1; for(i=1;i<=numa;i++) { if(is_correct(a[i])) cout<<a[i]<<" is correct"<<endl; else { memset(mark,0,sizeof(mark)); cout<<a[i]<<":"; if(judge(a[i])) { for(int y=1;y<=numl;y++) if(mark[y]) cout<<" "<<list[y]; }cout<<endl; } }}