• hdu1247 Hat’s Words


    Hat’s Words

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


    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
     
    题意:只有一个测试样例,输入到文件结束。从输入的单词中,找到一个单词是由两个输入的其他单词组成的单词,找出所有符合条件的单词。
     
    #include<iostream>
    #include<string.h>
    #include<string>
    using namespace std;
    char s[400005][26];
    int vis[400005],tree[400005][26];
    int n=0,num=0,root,id,len;
    void insert()
    {
        len=strlen(s[n]);
        root=0;
        for(int i=0;i<len;i++)
        {
            id=s[n][i]-'a';
            if(!tree[root][id])
                tree[root][id]=++num;//注意是++num
            root=tree[root][id];
        }
        vis[root]=1;//每个单词结束标记一下
    }
    int search(char *ss)
    {
        len=strlen(ss);
        root=0;
        for(int i=0;i<len;i++)
        {
            id=ss[i]-'a';
            if(tree[root][id])
                root=tree[root][id];
            if(vis[root]==1)
            {
                int root2=0;//根节点继续从0开始
                for(int j=i+1;j<len;j++)
                {
                    int id2=ss[j]-'a';
                    if(!tree[root2][id2])
                        break;
                    root2=tree[root2][id2];
                    if(vis[root2]==1&&j==len-1)
                        return 1;
                }
    
            }
    
        }
        return 0;
    }
    int main()
    {
        while(~scanf("%s",&s[++n]))
        {
            insert();
        }
        for(int i=1;i<=n;i++)
        {
            if(search(s[i]))
               printf("%s
    ",s[i]);
        }
        return 0;
    }
  • 相关阅读:
    分享一份Java架构师学习资料,2019年最新整理!
    Spring Boot 最核心的 25 个注解,都是干货!
    推荐一款接口 API 设计神器!
    题库
    杂乱的知识点
    mysql查询疯狂41例
    mysql你问我答
    可能出现的面试题
    SQLALchemy
    基于蓝图的完整的Flask项目
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11138940.html
Copyright © 2020-2023  润新知