首页 > 学院 > 开发设计 > 正文

poj 1035 Spell checker

2019-11-11 05:32:59
字体:
来源:转载
供稿:网友

字符串匹配,暴力可过

#include <cstring>#include <cstdio>char dic[10010][17];int wlen[10010];char Word[17];int dcnt;bool change(int index){ int len = strlen(word); int cnt = 0; for(int i = 0; i < len; ++i) { if(word[i] != dic[index][i]) ++cnt; if(cnt > 1) return false; } return true;}bool del(int index){ int len = strlen(word); int cnt,m; for(int i = 0; i < len; ++i) { cnt = m = 0; for(int j = 0; j < len; ++j) { if(j == i) continue; if(word[j] != dic[index][m++]) ++cnt; if(cnt) break; } if(!cnt) return true; } return false;}bool add(int index){ int len = strlen(dic[index]); int cnt,m; for(int i = 0; i < len; ++i) { cnt = m = 0; for(int j = 0; j < len; ++j) { if(i == j) continue; if(dic[index][j] != word[m++]) ++cnt; if(cnt) break; } if(!cnt) return true; } return false;}void check(){ int len = strlen(word); for(int i = 0; i < dcnt; ++i) if(len == wlen[i]) if(strcmp(dic[i],word) == 0) { PRintf("%s is correct/n",word); return; } printf("%s:",word); for(int i = 0; i < dcnt; ++i) { if(len == wlen[i]) { if(change(i)) printf(" %s",dic[i]); } else if(len-wlen[i] == 1) { if(del(i)) printf(" %s",dic[i]); } else if(len-wlen[i] == -1) { if(add(i)) printf(" %s",dic[i]); } } printf("/n");}int main(){ dcnt = 0; while(true) { scanf(" %s",dic[dcnt]); if(dic[dcnt][0] == '#') break; wlen[dcnt] = strlen(dic[dcnt]); ++dcnt; } while(true) { scanf(" %s",word); if(word[0] == '#') break; check(); } return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表