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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值