复合词 (Compound Words)(集合+字符函数)

╰+攻爆jí腚メ 2022-11-30 04:26 308阅读 0赞

题目描述

给出一个词典,找出所有的复合词,即恰好有两个单词连接而成的单词。输入每行都是一个小写字母组成的单词。输入已按照字典序从小到大排序,且不超过120000个单词。输出所有复合词,按照字典序从小到大排序。

输入输出样例

输入
  1. a
  2. alien
  3. born
  4. less
  5. lien
  6. never
  7. nevertheless
  8. new
  9. newborn
  10. the
  11. zebra
输出
  1. alien
  2. newborn
  3. #include<iostream>
  4. #include<cstring>
  5. #include<string>
  6. #include<set>
  7. using namespace std;
  8. set<string>st;
  9. int main()
  10. {
  11. for(string s;cin >> s;)
  12. {
  13. st.insert(s);
  14. }
  15. for(set<string>::iterator it = st.begin();it != st.end();++it)
  16. {
  17. for(int j = 1;j < (int)it->size();++j)
  18. {
  19. if(st.count(it->substr(0,j)) && st.count(it->substr(j,it->size()-j)))
  20. {
  21. cout<<*it<<endl;
  22. break;
  23. }
  24. }
  25. }
  26. return 0;
  27. }

发表评论

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

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

相关阅读