UVa 10391 - Compound Words

来源:互联网 发布:php程序员是什么 编辑:程序博客网 时间:2024/04/30 16:09

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 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

aalienbornlesslienneverneverthelessnewnewbornthezebra

Sample Output

aliennewborn

判断复合词,即该词能不能由已给出的词组成

例如,alien可由a与lien组成

用set ,记录每个词语,再枚举该词可以的组成形式


#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;#include<set>#include<map>int main(){    string a,b,c;    set<string>s;    set<string>::iterator p;    while(cin>>a)    {        s.insert(a);    }    int n,i,j,k;    p=s.begin();    while(p!=s.end()){        c=*p;///cout<<c<<endl;        n=c.length();        for(i=0;i<n-1;i++){            a.clear();            b.clear();            for(j=0;j<=i;j++)/// 得到第一个词a                  a+=c[j];            k=j;            for(j=0;k<n;j++,k++)/// 第二个词b                  b+=c[k];            ///cout<<a<<" "<<b<<"+++"<<endl;            if(s.count(a)&&s.count(b))   /// 枚举可以组成复合词的两个单词,判断它们是不是在集合中出现过            {                cout<<c<<endl;                break;            }        }        p++;    }}


0 0
原创粉丝点击