UVA 10391 Compound Words

来源:互联网 发布:网店美工培训课程 编辑:程序博客网 时间:2024/06/03 16:52

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

aalienbornlesslienneverneverthelessnewnewbornthezebra

Sample Output

aliennewborn

思路:

两种思路,合成和拆分。前者时间复杂度为O(n^2),超时。后者的为O(n*m),m小于n,VJ50ms,AC。
#include <iostream>#include <cstdlib>#include <string>#include <map>using namespace std;const int maxn=120000+5;int main(){    string word[maxn];    map<string,bool> jud;    int cnt=0;    while(cin>>word[cnt])        jud[word[cnt++]]=true;    for(int i=0;i<cnt;i++)    for(int j=0;j<(int)word[i].size()-1;j++)    {        string a=word[i].substr(0,j+1);        if(!jud[a]) continue;        string b=word[i].substr(j+1);        if(!jud[b]) continue;        cout<<word[i]<<endl;        break;    }    return 0;}


原创粉丝点击