poj 1035 Spell checker
题意:首先给定一些字符串(字典),然后再输入一个字符串,然后在字典里面查找看是否有,有则输出
没有则需要你纠正,纠正的原则是
替换一个字符
插入一个字符
删除一个字符
View Code
1 /* 2 poj1035 3 题意:给定一些字典里面的字符串,以#表示结束,然后再给定一个字符串str 4 若在字典里面能够找得到则输出:cout<<str<<" is correct"<<endl; 5 若没有,则按如下规则: 6 删除一个字母在字典里是否有 7 添加一个字母在字典里是否有 8 替换一个字母在字典里是否有. 9 10 */ 11 #include <iostream> 12 #include <string> 13 using namespace std; 14 string dir[10005]; 15 int dirlen[10005]; 16 string str; 17 int len; //str.length() 18 int fix[10005], f; 19 void Change(int k) 20 { 21 int pos = 0; 22 for (int i=0; i<len; i++) 23 { 24 if (str[i] != dir[k][i]) //不相同则替换掉. 25 pos ++; 26 if (pos == 2) 27 break; 28 } 29 if (pos != 2) 30 fix[f++] = k; 31 } 32 void Delete(int k) // len -1 == dirlen[k] 33 { 34 int pos = 0; 35 for (int i=0,j=0; i<len && j<dirlen[k]; i++, j++ ) 36 { 37 if (str[i] != dir[k][j]) 38 { 39 j--; 40 pos ++; 41 if (pos == 2) 42 break; 43 } 44 } 45 if (pos != 2) 46 fix[f++] = k; 47 } 48 void Add(int k) //len + 1 == dirlen[k] 49 { 50 int pos = 0; 51 for (int i=0,j=0; j<dirlen[k] && i<len; i++, j++) 52 { 53 if (str[i] != dir[k][j]) 54 { 55 i--; 56 pos ++; 57 if (pos == 2) 58 break; 59 } 60 } 61 if (pos != 2) 62 fix[f++] = k; 63 } 64 int main() 65 { 66 int n=0,i; 67 while (cin>>dir[n] && dir[n][0] != '#') 68 { 69 dirlen[n] = dir[n].length(); 70 n++; 71 } 72 while (cin>>str && str[0] != '#') 73 { 74 len = str.length(); 75 f = 0; 76 bool flg = false; 77 for (i=0; i<n; i++) 78 { 79 if (len == dirlen[i]) 80 { 81 if(str.compare(dir[i]) == 0) 82 { 83 flg = true; 84 break; 85 } 86 else 87 Change(i); 88 } 89 else if (len - 1 == dirlen[i]) 90 Delete(i); 91 else if (len + 1 == dirlen[i]) 92 Add(i); 93 } 94 if (flg) 95 cout<<str<<" is correct"<<endl; 96 else 97 { 98 cout<<str<<":"; 99 for (i=0; i<f; i++) 100 cout<<" "<<dir[fix[i]]; 101 cout<<endl; 102 } 103 } 104 return 0; 105 }