UVA 10391 Compound Words

比眉伴天荒 2022-06-07 02:21 199阅读 0赞

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 theconcatenation 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, inalphabetical order.

Sample Input

  1. a
  2. alien
  3. born
  4. less
  5. lien
  6. never
  7. nevertheless
  8. new
  9. newborn
  10. the
  11. zebra

Sample Output

  1. alien
  2. newborn

思路:

  1. 两种思路,合成和拆分。前者时间复杂度为O(n^2),超时。后者的为O(n*m),m小于nVJ50msAC
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <map>
  6. using namespace std;
  7. const int maxn=120000+5;
  8. int main()
  9. {
  10. string word[maxn];
  11. map<string,bool> jud;
  12. int cnt=0;
  13. while(cin>>word[cnt])
  14. jud[word[cnt++]]=true;
  15. for(int i=0;i<cnt;i++)
  16. for(int j=0;j<(int)word[i].size()-1;j++)
  17. {
  18. string a=word[i].substr(0,j+1);
  19. if(!jud[a]) continue;
  20. string b=word[i].substr(j+1);
  21. if(!jud[b]) continue;
  22. cout<<word[i]<<endl;
  23. break;
  24. }
  25. return 0;
  26. }

发表评论

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

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

相关阅读

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

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