当前位置: 代码迷 >> 综合 >> UVA - 10391 Compound Words (复合词)
  详细解决方案

UVA - 10391 Compound Words (复合词)

热度:35   发布时间:2023-10-15 12:49:39.0

题目
You are to ?nd 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.

代码

#include <iostream>
#include <set>
using namespace std;
set<string> dic, ans;
int main()
{
    // freopen("i.txt", "r", stdin);// freopen("o.txt", "w", stdout);string s;while(cin >> s) dic.insert(s);for(auto it : dic){
    s = it;for(int i = 1; i < s.length(); ++i){
    if(dic.count(s.substr(0, i)) && dic.count(s.substr(i))){
     ans.insert(s); break;}}}for(auto i : ans) cout << i << endl;return 0;
}
  相关解决方案