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;
    }
     
  • 相关阅读:
    Vue2+VueRouter2+webpack 构建项目实战(一):准备工作
    vue+webpack 安装常见插件
    JS与CSS阻止元素被选中及清除选中的方法总结
    IE浏览器的ActiveXObject对象以及FileSystemobject的应用扩展(完成)
    用webpack2.0构建vue2.0超详细精简版
    从淘宝和网易的font-size思考移动端怎样使用rem?
    用CSS开启硬件加速来提高网站性能
    JS实现数组去重方法整理
    [总结]高效的jQuery代码编写技巧
    JS apply的巧妙用法以及扩展到Object.defineProperty的使用
  • 原文地址:https://www.cnblogs.com/yu0111/p/4727696.html
Copyright © 2011-2022 走看看