POJ1035http://poj.org/problem?id=1035
前:
(噗...)
题目大意:
先给定一个字典,然后给出一些询问,都是单个单词,查询在字典中的
1.一模一样的单词 , 输出"correct";
2.差距在一个字母之内,且可删除的如mre = me;
3.差距在一个字母之内,且可增添的如mre = more;
思路:不用特定判断增添/删除操作,只需判断是否单词长度差距为一且仅有一个单词差距即可。
TLE提示:慎重使用String类..
???????AC代码:
#include <iostream>
#include <cstring>
using namespace std;
char s[10005][20],s1[55][20];
bool add(char *s3,char *s2) { //注意S3与S2长度,下同int ans = 0;while(*s3) {if(*s3 != *s2) {s3++;ans++;if(ans > 1) return false;}else {s3++;s2++;}}return true;
}
bool del(char *s3,char *s2) {int ans = 0;while(*s2) {if(*s3 != *s2) {s2++;ans++;if(ans > 1) return false;}else {s3++;s2++;}}return true;
}
bool rep(char *s3,char *s2) {int ans = 0;while(*s3) {if(*(s3++) != *(s2++)) {ans++;if(ans > 1) return false;}}return true;
}
int main() {int size = 0,size1 = 0;while(cin >> s[size]) {if (s[size][0] == '#') break;size++;}while(cin >> s1[size1]) {if (s1[size1][0] == '#') break;int len1 = strlen(s1[size1]);int f = 0;for (int i = 0; i < size; i++) {if (!strcmp(s[i], s1[size1])) {cout << s1[size1] << " is correct" << endl;f = 1;break;}}if (f) continue;cout << s1[size1] << ":";for (int i = 0; i < size; i++) {int len = strlen(s[i]);if (len1 + 1 == len) {if (del(s1[size1], s[i])) {cout << " " << s[i];}}if (len1 == len) {if (rep(s1[size1], s[i])) {cout << " " << s[i];}}if (len1 - 1 == len) {if (add(s1[size1], s[i])) {cout << " " << s[i];}}}cout << endl;size1++;}return 0;
}