Hat’s Words

来源:互联网 发布:淘宝制作主图ps视频 编辑:程序博客网 时间:2024/05/19 02:42

Hat’s Words

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 40   Accepted Submission(s) : 12

Font: Times New Roman | Verdana | Georgia

Font Size:  

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

aahathathatwordhzieeword

Sample Output

ahathatword

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;




char a[50005][100];




typedef struct Node
{
    Node *child[26];
    bool n;
}*tree;




void Insert (tree root, char *str)
{
    int len = strlen (str);
    tree ans = root,p;
    for (int i =0; i < len; i ++)
    {
       int t=str[i]-'a';
       if(ans->child[t]!=NULL)
       {
           ans=ans->child[t];
       }
       else
       {
           p=(tree)calloc(1,sizeof(Node));
           p->n=0;
           ans->child[t]=p;
           ans=p;
       }
    }
    ans->n=1;
}
bool find (tree root,char *str,int x,int y)
{
    tree ans = root;
    int i,q;
    for (i=x;i<y;i++)
    {
        q=str[i]-'a';
        if(ans->child[q]!=NULL)
            ans=ans->child[q];
        else
            return 0;
    }
    return ans->n;
}
void releas(tree root)
{
    int i;
    for(i=0;i<26;i++)
        if(root->child[i]!=NULL)
           releas(root->child[i]);
    free(root);
}
int main()
{ int n=0,i,j;
tree root=(tree)calloc(1,sizeof(Node));
 root->n=0;
 while(scanf("%s",a[n])!=EOF)
 {
     Insert(root,a[n++]);
 }
 for(i=0;i<n;i++)
 {
     int len=strlen(a[i]);
     for(j=1;j<len-1;j++)
     {
         if(find(root,a[i],0,j)&&find(root,a[i],j,len))
         {
             printf("%s\n",a[i]);
             break;
         }
     }
 }
 releas(root);
    return 0;
}

原创粉丝点击