UVA 10391 Compound Words

àì夳堔傛蜴生んèń 2022-01-06 11:45 213阅读 0赞

思路:

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

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

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

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

转载于:https://www.cnblogs.com/fudanxi/p/10382305.html

发表评论

表情:
评论列表 (有 0 条评论,213人围观)

还没有评论,来说两句吧...

相关阅读

    相关 UVA - 10924 - Prime Words (素数)

    输入一个由大小写字母组成的字符串,每个字符代表着不同的数字,计算出这个字符串的数值,判断是否是素数; 首先我们打个素数表; 然后利用ascll码存入数组中,然后判断就o