题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1825
Time Limit: 5 Seconds Memory Limit: 32768 KB

Description

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

a
alien
born
less
lien
never
nevertheless
new
newborn
the
zebra

Sample Output

alien
newborn

Problem solving report:

Description: 找出由其他任意两个单词连接起来组成的符合词。
Problem solving: 先把字典存到set里面,然后对于每个单词,暴力地分成两个子串查找即可。

Accepted Code:​​​​​​​

#include <bits/stdc++.h>
using namespace std;
int main() {
    char str[100];
    set <string> spt;
    while (~scanf("%s", str)) {
        spt.insert(str);
    }
    set <string>::iterator it;
    for (it = spt.begin(); it != spt.end(); it++) {
        for (int i = 1; i < it -> size(); i++) {
            string s1(*it, 0, i);
            string s2(*it, i);
            if (spt.count(s1) && spt.count(s2)) {
                printf("%s\n", it -> c_str());
                break;
            }
        }    
    }
    return 0;
}