zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    ltp-ddt makefile的思考
    Linux configure,make,make install
    linux下can调试工具canutils安装过程记录
    windows下运行jar
    悲观锁
    mysql事务锁表
    静态内部类
    局部类
    匿名内部类(new类时覆盖类中方法)
    回文字
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11138940.html
Copyright © 2011-2022 走看看