hdu 1247 Hat’s Words Trie树

Hat’s Words

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


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
 

Author
戴帽子的
 

Recommend
Ignatius.L



题意:把单词分成两个,然后判断是不是为出现的单词,直接把单词建树,然后把单词分成两个部分找到即可。




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

const int N=26;
char str[50005][20];
typedef struct Trie
{
    int v;//根据需要变化
    Trie *next[26];
} Trie;
Trie *root;

void createTrie(char *str)
{
    int len = strlen(str);
    Trie *p = root, *q;
    for(int i=0; i<len; ++i)
    {
        int id = str[i]-'a';
        if(p->next[id] == NULL)
        {
            q = (Trie *)malloc(sizeof(Trie));
            q->v = 1;//初始v=1
            for(int j=0; j<26; ++j)
                q->next[j] = NULL;
            p->next[id] = q;
        }
            p = p->next[id];
    }
    // p->v = -1;   //若为结尾,则将v改成-1表示(视情况而定)
    p->v = -1;
}

int findTrie(char *str)
{
    int len = strlen(str);
    Trie *p = root;
    for(int i=0; i<len; ++i)
    {
        //根据需要选择是减去'0'还是'a',或者是'A'
        int id = str[i]-'a';
        p = p->next[id];
        //若为空集,表示不存以此为前缀的串
        if(p == NULL)
            return 0;
    }
        //字符集中已有串是此串的前缀
        if(p->v == -1)
            return -1;
     //此串是字符集中某串的前缀
    return 0;
}
int deal(Trie* T)
{
    //这是把T清空,一开始没加这个,结果MLE
    int i;
    if(T==NULL)
        return 0;
    for(i=0; i<N; i++)
    {
        if(T->next[i]!=NULL)
            deal(T->next[i]);
    }
    free(T);
    return 0;
}

char a[105],b[105];

int main()
{
    int i,j,n;
    root = (Trie *) malloc (sizeof(Trie));
    for(int i=0; i<N; i++)
        root->next[i]=NULL;
    n=0;
    while(scanf("%s",str[n])!=EOF)
    {
        createTrie(str[n]);
        n++;
    }
    for(i=0; i<n; i++)
    {
        int len=strlen(str[i]);
        for(j=0; j<len; j++)
        {
            memset(a,'\0',sizeof(a));
            memset(b,'\0',sizeof(b));
            strncpy(a,str[i],j);
            strncpy(b,str[i]+j,len-j);
            if(findTrie(a)==-1&&findTrie(b)==-1)
            {
                printf("%s\n",str[i]);
                break;
            }
        }
    }
    deal(root);
    return 0;
}




 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值