码迷,mamicode.com
首页 > 其他好文 > 详细

Hat’s Words

时间:2014-08-10 10:23:50      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   os   io   for   ar   cti   

Hat’s Words

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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
# include<cstring>
using namespace std;
#define MAX 51000
map<string,int> m;
 string ch[MAX];
int main()
{
    char ch1[100],ch2[100];
    int i=0;

    while(cin>>ch[i])
    {
       m[ch[i++]]=1;
    }
    for(int j=0;j<i;j++)
    {
        for(int k=1;k<ch[j].size()-1;k++)
        {
             string ch1(ch[j],0,k);     //表示把ch[j]中从下标0开始连续的k个字符串赋给ch1  
             string ch2(ch[j],k,ch[j].size()-k);
            if(m[ch1]==1&&m[ch2]==1)
            {
               cout<<ch[j]<<endl;
                break;
            }
        }
    }
    return 0;
}


 

Hat’s Words,布布扣,bubuko.com

Hat’s Words

标签:des   style   color   os   io   for   ar   cti   

原文地址:http://blog.csdn.net/u013514722/article/details/38467911

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!