题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description

A hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.
You are to find all the hat’s words in a dictionary.

Input

Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
Only one case.

Output

Your output should contain all the hat’s words, one per line, in alphabetical order.

Sample Input

a
ahat
hat
hatword
hziee
word

Sample Output

ahat
hatword

Problem solving report:

Description: “帽子单词”是指,若字典中的某个词,它是由其他任意两个单词连接起来组成的,则称它为“帽子单词”。
现在以字典序给出字典中的所有单词(不超过5e4个),让你求出全部“帽子单词”。
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++) {
        string a = *it;
        for (int j = 1; j < a.size(); j++) {
            string s1(a, 0, j);
            string s2(a, j);
            if (spt.find(s1) != spt.end() && spt.find(s2) != spt.end()) {
                printf("%s\n",a.c_str());
                break;
            }
        }
    }
    return 0;
}