zoukankan      html  css  js  c++  java
  • hdu 1247 Hat’s Words Trie树(+测试数据)

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

    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
    ",s[j]);
        }
        //del(root);
        return 0;
    }
     
  • 相关阅读:
    洛谷P1020/CODEVS1044 导弹拦截(拦截导弹)
    洛谷P1541/CODEVS1068 乌龟棋
    洛谷1791/CODEVS1214线段覆盖
    NOIP2002提高组/洛谷P1031均分纸牌
    【USACO2009Decsilver T1 自私的食草者
    洛谷P1024/NOI题库7891(2.3)/NOIP2001提高组T1 一元三次方程求解
    洛谷1086/NOI题库1.13.38/NOIP2004普及组第2题 花生采摘
    NOIP2010/洛谷P1525关押罪犯
    洛谷P1115最大子段和
    1244-作为一个java开发者的知识储备
  • 原文地址:https://www.cnblogs.com/yu0111/p/4727696.html
Copyright © 2011-2022 走看看