题目
You are to ?nd 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.
代码
#include <iostream>
#include <set>
using namespace std;
set<string> dic, ans;
int main()
{
// freopen("i.txt", "r", stdin);// freopen("o.txt", "w", stdout);string s;while(cin >> s) dic.insert(s);for(auto it : dic){
s = it;for(int i = 1; i < s.length(); ++i){
if(dic.count(s.substr(0, i)) && dic.count(s.substr(i))){
ans.insert(s); break;}}}for(auto i : ans) cout << i << endl;return 0;
}