POJ-1035 Spell checker---字符串处理

题目链接:

https://vjudge.net/problem/POJ-1035

题目大意:

输入一部字典,输入若干单词

1、  若某个单词能在字典中找到,则输出corret

2、  若某个单词能通过 变换删除添加一个字符后,在字典中找得到,则输出这些单词,输出顺序根据  输入的那部字典的字典序

3、  若某个单词无论操作与否都无法在字典中找得到,则输出空

解题思路:

直接模拟,暴力求解,但是玄学在于用c++提交就过了,G++提交就超时,应该是代码中用了STL和string的原因,感觉直接用c语言的char二维数组和strcmp函数的话应该都可以过

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<cstring>
 5 using namespace std;
 6 const int maxn = 1e4 + 10;
 7 string s[maxn];
 8 bool judge(string x, string y)
 9 {
10     int tot = 0;
11     //cout<<x<<" "<<y<<endl;
12     if(x.size() == y.size())
13     {
14         for(int i = 0; i < x.size(); i++)
15         {
16             if(x[i] != y[i])tot++;
17         }
18         return tot == 1;
19     }
20     else
21     {
22         if(x.size() < y.size())swap(x, y);
23         for(int i = 0, j = 0; i < x.size() && j < y.size();)
24         {
25             if(x[i] == y[j])
26             {
27                 i++;
28                 j++;
29             }
30             else
31             {
32                 i++;
33                 tot++;
34             }
35         }
36         return tot <= 1;
37     }
38 }
39 int main()
40 {
41     int tot = 0;
42     std::ios::sync_with_stdio(false);
43     string s1;
44     while(cin >> s1)
45     {
46         if(s1[0] == '#')break;
47         s[tot++] = s1;
48     }
49     vector<string>ans;
50     while(cin >> s1)
51     {
52         if(s1[0] == '#')break;
53         int flag = 0;
54         ans.clear();
55         for(int i = 0; i < tot; i++)
56         {
57             if(s[i] == s1)
58             {
59                 flag = 1;
60                 break;
61             }
62             else if(s[i].size() == s1.size() || s[i].size() - s1.size() == 1 || s1.size() - s[i].size() == 1)
63             {
64                 if(judge(s[i], s1))ans.push_back(s[i]);
65             }
66         }
67         cout<<s1;
68         if(flag == 1)
69             cout<<" is correct"<<endl;
70         else
71         {
72             cout<<":";
73             for(int i = 0; i < ans.size(); i++)
74                 cout<<" "<<ans[i];
75             cout<<endl;
76         }
77     }
78     return 0;
79 }
原文地址:https://www.cnblogs.com/fzl194/p/8911336.html