HDU1247Hat’s Words--Trie

Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5789    Accepted Submission(s): 2167


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

分析:建立一颗Trie树,把这些字符串存起来,然后暴力拆分字符串,与Trie树上面的字符串进行比较,或者说查找吧,如果拆分出来的两部分都能够找到,那么成功输出原字符串,否则失败。。
/*************************************************************************
* author:crazy_石头
* algorithm:Trie
* date:2013/09/29
**************************************************************************/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
 

struct TrieNode
{
    bool ok;
    struct TrieNode *next[26];
};
 

TrieNode *root=new TrieNode;

char s[50000][100];
 

void build(char *s)
{
    TrieNode *p=root,*q;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        if(p->next[s[i]-'a']==NULL)
        {
            q=new TrieNode;
            for(int j=0;j<26;j++)
            {
                q->next[j]=NULL;
                q->ok=false;
            }

            p->next[s[i]-'a']=q;
            p=q;
        }
        else
        {
            p=p->next[s[i]-'a'];
        }
    }
    p->ok=true;
}
 

int Search(char *s)
{
    TrieNode *p=root,*q;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        if(p->next[s[i]-'a']!=NULL)
        {
            p=p->next[s[i]-'a'];
        }
        else
        {
            return 0;
        }
    }
    if(p->ok)
        return 1;
    else
        return 0;
}
 

int main()
{
    char s1[100],s2[100];

    for(int i=0;i<26;i++)
    {
        root->next[i]=NULL;
        root->ok=0;
    }

    int cur_len=0;
    while(scanf("%s",s[cur_len])!=EOF)
    {
        build(s[cur_len]);
        cur_len++;
    }

    for(int ii=0;ii<cur_len;ii++)
    {
        int len=strlen(s[ii]);
 

        for(int i=0;i<len-1;i++)
        {
            int s1_len=0,s2_len=0;
            for(int jj=0;jj<=i;jj++)
            {
                s1[s1_len]=s[ii][jj];
                s1_len++;
            }
 

            for(int j=i+1;j<len;j++)
            {
                s2[s2_len]=s[ii][j];
                s2_len++;
            }

            s1[s1_len]=s2[s2_len]='\0';

            if(Search(s1)&&Search(s2))
            {
                puts(s[ii]);
                break;
            }

            memset(s1,0,sizeof(s1));
            memset(s2,0,sizeof(s2));

        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值