zoukankan      html  css  js  c++  java
  • 【hdu1247】Hat’s Words

    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

    Author

    戴帽子的

    题解

    题目意思:给一堆串,判断哪个串可以又这堆串里的两个串相连得到

    trie树。把串先读进去,然后枚举每种可能判断一下。

    s.substr (i,j) 串s 从i开始截取j位

    next要开27个。(0里边是空的)

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    struct node
    {
        int next[27];
        int w;
    }t[50010];
    string ss[50010];
    string s;
    int topt,cnt;
    void add_trie()
    {
        int l=s.length(),now = 0;
        for (int i=0;i<l;i++)
        {
            int x = s[i]-'a'+1;
            if (t[now].next[x])
                now = t[now].next[x];
            else 
            {
                t[now].next[x] = ++topt;
                now = topt;
            }
        }
        t[now].w=1;
    }
    int find()
    {
        int l=s.length(),now = 0,p = 0;
        while (p<l)
        {
            if (!t[now].next[s[p]-'a'+1]) return 0;
            else
            {
                now = t[now].next[s[p]-'a'+1];
                p++;
            }
        }
        if (t[now].w) return 1;
            else return 0;
    }
    int main()
    {
        while (cin>>ss[++cnt])
        {
            s = ss[cnt];
            add_trie();
        }
        for (int i=1;i<cnt;i++)
        {
            string s3=ss[i];
            int l=s3.length();
            for (int j=1;j<l;j++)
            {
                s=s3.substr(0,j);
                if (find())
                {
                    s = s3.substr(j,l);
                    if (find())
                    {
                        cout<<s3<<endl;
                        break;
                    }
                }
            }
        }    
    }
  • 相关阅读:
    POJ 2155 Matrix(二维树状数组)
    HDU 1280 前m大的数
    HDU 3183 A Magic Lamp(二维RMQ)
    HDU 3743 Frosh Week(归并排序求逆序数)
    POJ 2299 Ultra-QuickSort ( 归并排序 + 求逆序数 )
    HDU 1166 敌兵布阵(树状数组)
    HDU 2846 Repository(字典树)
    HDU 1896 Stones(优先队列)
    HDU 4393 Throw nails(优先队列)
    进程池
  • 原文地址:https://www.cnblogs.com/liumengyue/p/5468784.html
Copyright © 2011-2022 走看看