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): 21250    Accepted Submission(s): 7449

    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<cstring>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    const int maxn=1000005;
    int trie[maxn][27];
    int color[maxn];
    int k=1;
    int cnt=0;
    char word[50005][50];
    using namespace std;
    void _insert(char *w)
    {
        int p=0;
        int len=strlen(w);
        for(int i=0;i<len;i++)
        {
            int temp=w[i]-'a';
            if(!trie[p][temp])
            {
                trie[p][temp]=k++;
            }
            p=trie[p][temp];
        }
        color[p]=1;
    }
    int query(char *w)
    {
        int p=0;
        int len=strlen(w);
        for(int i=0;i<len;i++)
        {
            int temp=w[i]-'a';
            if(trie[p][temp])
            {
                p=trie[p][temp];
                if(color[p])//如果某个字符点是终结点,则从下一个字符组成的单词从树中查找。
                {
                    int pos=0;
                    for(int j=i+1;j<len;j++)
                    {
                        int temp2=w[j]-'a';
                        if(!trie[pos][temp2])
                        {
                            break;
                        }
                        pos=trie[pos][temp2];
                        if(j==len-1&&color[pos]==1)
                        {
                            return 1;
                        }
                    }
                }
            }
            else
                return 0;
        }
        return 0;
    }
    int main()
    {
        while(scanf("%s",word[cnt])!=EOF)//一开始的单词即为字典序输入,所以可以直接输出.
        {
            _insert(word[cnt]);
            cnt++;
        }
        for(int i=0;i<cnt;i++)
        {
            if(query(word[i]))
            {
                printf("%s
    ",word[i]);
            }
        }
    }
    View Code
  • 相关阅读:
    zw版【转发·台湾nvp系列Delphi例程】HALCON CropPart
    zw版【转发·台湾nvp系列Delphi例程】HALCON ObjToInteger1-4
    zw版【转发·台湾nvp系列Delphi例程】HALCON TestObjDef
    zw版【转发·台湾nvp系列Delphi例程】HALCON DispCross
    ios-toolchain-based-on-clang-for-linux
    使用gdb调试theos tweak插件
    设置RabbitMQ远程ip登录
    ARC下带CF前缀的类型与OC类型转换
    laravel部署常用命令
    国产手机插入mac os 系统中无法被识别的解决方法
  • 原文地址:https://www.cnblogs.com/switch-waht/p/11519456.html
Copyright © 2011-2022 走看看