HDU 1247

Hat’s Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3779    Accepted Submission(s): 1432


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
 1 //大致题意: 判断一个单词是否由其他两个单词组成 
 2 #include <iostream>
 3 #include <vector>
 4 #include <string>
 5 #include <cstring>
 6 #include <map>
 7 using namespace std;
 8 int main()
 9 {
10     int i,j,k;
11     string s="";
12     vector <string > v;
13     map <string ,int > mm;
14     while(cin>>s)
15     {
16         v.push_back(s);
17         mm[s]++;//不为0表示存在 
18         s.clear();
19     }
20     for(i=0;i<v.size();i++)
21     {
22         string s1="";
23         s1=v[i];
24         string s2 = "";//不可放在内层 
25         for(j=0;j+1<s1.size();j++)
26         {
27             s2 += s1[j];
28             string s3 = s1.substr(j+1);
29             if(mm[s2]&&mm[s3])
30             {
31                 cout<<s1<<endl;
32                 s2.clear();
33                 break;//可能一个单词有多种匹配,满足一种输出即可 
34             }
35         }
36     }
37     system("pause");
38     return 0;
39 }
40                 
41             
42             

 

posted @ 2012-08-20 11:56  加拿大小哥哥  阅读(313)  评论(0编辑  收藏  举报