hdu 1247 Hat’s Words 字典树

点击打开链接题目链接

Hat’s Words

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


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
 

在输入的字符串中 如果一个字符由其余的两个字符组成  则称为hat‘s words

输出所有的hat‘s words

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
using namespace std;
struct node
{
    node *next[26];
    int end;
};
node *head;
char str[50500][100];
void tree_add(char *str)
{
    node *t,*s=head;
    int i,j;
    int l=strlen(str);
    for(i=0;i<l;i++)
    {
        int id=str[i]-'a';
        if(s->next[id]==NULL)
        {
            t=new node;
            for(j=0;j<26;j++)
            {
                t->next[j]=NULL;
            }
            t->end=0;
            s->next[id]=t;
        }
        s=s->next[id];
    }
    s->end=1;
}
int tree_search(char *str)
{
    int l=strlen(str);
    int i,j,id;
    node *s=head;
    for(i=0;i<l;i++)
    {
        id=str[i]-'a';
        if(s->next[id]==NULL)
            return 0;
        s=s->next[id];
    }
    return s->end;
}

int main()
{
    int n=0;
    head=new node;
    int i,j;
    char left[100],right[100];
    for(i=0;i<26;i++)
    {
        head->next[i]=NULL;
    }
    head->end=0;
    while(gets(str[n])!=NULL&&strcmp(str[n],"")!=0)
    {
        tree_add(str[n++]);
    }
    for(i=0;i<n;i++)
    {
        int l=strlen(str[i]);
        for(j=1;j<l;j++)
        {
            strcpy(left,str[i]);
            left[j]='\0';
            strcpy(right,str[i]+j);
            if(tree_search(left)&&tree_search(right))
            {
                puts(str[i]);
                break;
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值