标签:style blog http io ar color os sp for
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.
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 120,000 words.
Your output should contain all the compound words, one per line, in alphabetical order.
a alien born less lien never nevertheless new newborn the zebra
alien newborn
思路很简单,将一个单词拆成好两个单词,然后搜索是否存在这两个单词就可以,注意break;
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <map> 5 #include <vector> 6 7 using namespace std; 8 9 map<string,int> IDcatch; 10 vector<string> v; 11 int main () { 12 string str; 13 while (cin >> str) { 14 v.push_back(str); 15 IDcatch[str] = 1; 16 } 17 for (int i = 0;i < v.size();i++) { 18 for (int j = 0,len = v[i].length();j < len;j++) { 19 if (IDcatch.count(v[i].substr(0,j)) && IDcatch.count(v[i].substr(j,len - j))) { 20 cout << v[i] << endl; 21 break; 22 } 23 } 24 } 25 }
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/xiaoshanshan/p/4106486.html