紫书第五章训练2 F - Compound Words

悠悠 2021-12-16 12:49 330阅读 0赞

F - Compound Words

You are to find all the two-word compound words in a dictionary. A two-word compound word is a
word in the dictionary that is the concatenation of exactly two other words in the dictionary.

Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will
be no more than 120,000 words.

Output
Your output should contain all the compound words, one per line, in alphabetical order.

Sample Input
a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra

Sample Output
alien
newborn

这个题就是找个单词是已出现两个单词的和,我用map写一直错, 我分析了下大概是每次都要访问map,然后map的空间就不够了,所以还是用set搞下不错,他和map的查询复杂度都是logn的,运用string还有find岂不是很简单实用,美滋滋

不过需要提出的是字典树或者直接数组搞会比stl速度快些的

ContractedBlock.gif ExpandedBlockStart.gif

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. string s;
  5. set<string>ma;
  6. while(cin>>s){
  7. ma.insert(s);
  8. }
  9. set<string>::iterator it;
  10. for(it=ma.begin();it!=ma.end();it++){
  11. string c=*it;
  12. for(int i=1;c[i];i++){
  13. if(ma.find(c.substr(0,i))!=ma.end()&&ma.find(c.substr(i))!=ma.end()){
  14. cout<<c<<endl;
  15. break;
  16. }
  17. }
  18. }
  19. return 0;}

转载于:https://www.cnblogs.com/BobHuang/p/6842868.html

发表评论

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

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

相关阅读

    相关 《以为友

    带着上一个领域的知识积木进入下一个、再下一个领域时,这些知识汇聚到一起,形成了一个可怕的复杂系统——大师思想从中诞生。不能解决当下问题的,降低关注度。并不是否认这个知识好...