首页 > 其他 > 详细

UVA 10391 Compound Words

时间:2019-02-15 10:59:28      阅读:177      评论:0      收藏:0      [点我收藏+]

思路:

  遍历拼接会超限,但是可以往下拆解;用一个map<string,bool>存一个单词是否是输入的(true),

  遍历拆解单词,寻找它拆分出的两个词s1,s2有没有在map里面值为true;如果是,就装到

  set里面(因为题目要求字典序输出),最后输出结果。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<cstring>
 5 #include<set>
 6 #include<vector>
 7 #include<map>
 8 #define maxn 120005
 9 using namespace std;
10 string word[maxn];
11 map<string, bool> dic;
12 set<string> res;
13 int main()
14 {
15     string str;
16     int cnt = 0;
17     while (getline(cin, str))
18     {
19         dic[str] = true;
20         word[cnt++] = str;
21     }
22 
23     for (int i = 0; i < cnt; i++)
24     {
25         for (int j = 0; j < word[i].length(); j++)
26         {
27             string s1 = word[i].substr(0, j);
28             string s2 = word[i].substr(j);
29             if (dic[s1] && dic[s2])
30             {
31                 
32                 res.insert(word[i]);
33             }
34         }
35     }
36 
37     for (set<string>::iterator it = res.begin(); it != res.end(); it++)
38         cout << *it << endl;
39     
40     return 0;
41 }

 

UVA 10391 Compound Words

原文:https://www.cnblogs.com/fudanxi/p/10382305.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!