Compound Words (UVA - 10391)

版权声明:欢迎转载!拒绝抄袭. https://blog.csdn.net/qq_36257146/article/details/87459597

Problem E: 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 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
a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra
Sample Output
alien
newborn
 

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

set<string>word;

int main()
{
    string s;
    set<string>::iterator it;
    while(cin>>s)
    {
        word.insert(s);
    }
    for(it = word.begin();it!=word.end();it++)
    {
        string t = *it;
        int len = (*it).length();
        for(int i = 1;i<len;i++)
        {
            string s1 = t.substr(0,i);
            string s2 = t.substr(i,len-i);
            //cout<<s1<<" "<<s2<<endl;
            if(word.find(s1)!=word.end() && word.find(s2)!=word.end())
            {
                cout<<*it<<endl;
                break;
            }
        }

    }
    return 0;
}
/**
a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra
**/

猜你喜欢

转载自blog.csdn.net/qq_36257146/article/details/87459597