zoukankan      html  css  js  c++  java
  • HDU 1247

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247

    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

    题意:

    “帽子单词”是指,若字典中的某个词,它是由其他任意两个单词连接起来组成的,则称它为“帽子单词”。

    现在以字典序给出字典中的所有单词(不超过 $5e4$ 个),让你求出全部“帽子单词”。

    题解:

    先把字典建成字典树,然后对于每个单词,暴力地分成两个子串查找即可。

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=5e4+5;
    
    namespace Trie
    {
        const int SIZE=maxn*32;
        int sz;
        struct TrieNode{
            int ed;
            int nxt[26];
        }trie[SIZE];
        void init(){sz=1;}
        void insert(const string& s)
        {
            int p=1;
            for(int i=0;i<s.size();i++)
            {
                int ch=s[i]-'a';
                if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
                p=trie[p].nxt[ch];
            }
            trie[p].ed++;
        }
        int search(const string& s)
        {
            int p=1;
            for(int i=0;i<s.size();i++)
            {
                p=trie[p].nxt[s[i]-'a'];
                if(!p) return 0;
            }
            return trie[p].ed;
        }
    };
    int tot;
    string s[maxn];
    int main()
    {
        ios::sync_with_stdio(0);
        cin.tie(0), cout.tie(0);
        Trie::init();
        tot=0;
        while(cin>>s[++tot]) Trie::insert(s[tot]);
        for(int i=1;i<=tot;i++)
        {
            bool ok=0;
            for(int k=1;k<s[i].size();k++)
            {
                if(Trie::search(s[i].substr(0,k)) && Trie::search(s[i].substr(k,s[i].size()-k)))
                {
                    ok=1;
                    break;
                }
            }
            if(ok) cout<<s[i]<<'
    ';
        }
    }
  • 相关阅读:
    Docker(12)- docker run 命令详解
    adb 常用命令大全(7)- 其他实用功能
    adb 常用命令大全(6)- 模拟按键输入
    adb 常用命令大全(5)- 日志相关
    adb 常用命令大全(4)- 应用管理
    adb 常用命令大全(3)- 查看手机设备信息
    adb 常用命令大全(2)- 基础命令
    adb 常用命令大全(1)- 汇总
    Docker
    Docker(11)- docker ps 命令详解
  • 原文地址:https://www.cnblogs.com/dilthey/p/9974165.html
Copyright © 2011-2022 走看看