hdu 1247 Hat’s Words Trie树(+测试数据)

                      Hat’s Words
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 1247

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
 
 
题意:在一些字符串中,找到这样字符串:由两个其他的字符串构成(也可能是由同一个单词两次构成 )!
祝猿们AC愉快!第一次写博客....文笔不好。 只因这题虐我伤痕累累,(呜呜~>_<~+)好不容易写出来了,就总结了一下..这也预示着我的博客之旅的开启!   √(─皿─)√

自己写的几组测试数据:
Input:
a
aa
aaa
Output:
aa
aaa
Input:
ab
cd
abcdef
abcd
Output:
abcd
 
 
 
 
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#define N 26
#define PI acos(-1.0)
using namespace std;
typedef struct Trie
{
    int sum;
    int flag;
    struct Trie *next[N];
};
Trie *root=NULL;
Trie *Newnode()/*初始化树*/
{
    Trie *p=(Trie *)malloc(sizeof(Trie));
    p->sum=0;
    p->flag=0;
    for(int i=0; i<N; i++)
        p->next[i]=NULL;
    return p;
}
void creatTire(char *s)/*建树   这个可以作为模版*/
{
    int i,len=strlen(s);
    Trie *p=root;
    for(i=0; i<len; i++)
    {
        int k=s[i]-'a';
        if(p->next[k]==NULL)
            p->next[k]=Newnode();
        p=p->next[k];
        p->sum++;
    }
    p->flag=1;/*标记一个字符串的最后一个单词*/
}
void del(Trie *p)/*清除树*/
{
    int i;
    if(p==NULL)
        return ;
    for(i=0; i<N; i++)
        if(p->next[i]!=NULL)
            del(p->next[i]);
    free(p);
}
int findTire(char *s)
{
    Trie *p=root;
    int n=strlen(s),i;
    for(i=0; i<n; i++)
    {
        int j,k;
        k=s[i]-'a';
        p=p->next[k];
        if(p==NULL)
            break;
        if(p->flag==1&&i!=n-1)/*找到了匹配的前半部分*/
        {
            j=i+1;
            Trie *q=root;
            for(; j<n; j++) /*找后半部分*/
            {
                k=s[j]-'a';
                q=q->next[k];
                if(q==NULL)
                    break;
                if(q->flag==1&&j==n-1)/*j==n-1表示完全匹配*/
                    return 2;/*返回2说明找到符合题意的了*/
            }
            //return 1;/*在这错了好久,要是找到了前半部分但是没找到匹配的后半部分应该把前半部分的下标向后移,继续判断!*/
        }
    }
    return 0;
}
char s[51000][110];
int main()
{
    int n,i=0,j;
    root=Newnode();/*这儿要注意了  别忘了加上这一句*/
    while(scanf("%s",s[i])!=EOF)/*有一些人说用ges错  就不太清楚了*/
    {
        creatTire(s[i]);
        i++;
    }
    for(j=0; j<i; j++)
    {
        n=findTire(s[j]);
        if(n==2)
            printf("%s\n",s[j]);
    }
    //del(root);
    return 0;
}

 

 

转载于:https://www.cnblogs.com/yu0111/p/4727696.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值