zoukankan      html  css  js  c++  java
  • HDU Problem 1247 Hat's Words 【字典树】

    Hat’s Words

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

    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
     
    Author
    戴帽子的
     
    Recommend
    Ignatius.L   |   We have carefully selected several similar problems for you:  1075 1298 1800 2846 1305 

    题意:给你n个单词,处理到EOF。问这些单词中有几个单词是由两个拼接成的

    思路:

    用字典树,枚举每个单词,找到这个单词每个节点上的单词结尾标志,记录,然后检查剩下的单词出没出现在字典树上。

    #include <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    typedef long long LL;
    typedef __int64 Int;
    typedef pair<int, int> paii;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-5;
    const double PI = acos(-1.0);
    const int MOD = 1e9 + 7;
    const int MAXN = 100 + 10;
    char str[50000 + 10][30];
    // 字典树
    struct trie {
        int cnt; //记录该节点下的单词数目
        trie* next[30]; //以每个单词创建子树
        bool endd; //该节点是否为单词的最后一位
    } *root;
    //创建根节点
    void build_trie() {
         root = new trie;
         for(int i = 0;i < 27; i++)
              root->next[i] = NULL;
    }
    void insert_trie(char s[]) {
        int len = strlen(s);
        trie *p = root, *q;
        for (int i = 0; i < len; i++) {
            int idx = s[i] - 'a';
            //如果该字母没有出现
            if (p->next[idx] == NULL) {
                q = new trie;
                q->cnt = 0;
                q->endd = false;
                for (int j = 0; j < 30; j++) {
                    q->next[j] = NULL;
                }
                p->next[idx] = q;
            }
            p = p->next[idx];
            //记录此时的单词数目
            p->cnt++;
        }
        //标记结尾
        p->endd = true;
    }
    bool query_trie(char s[]) {
        trie *p = root;
        int len = strlen(s);
        int num[50000];
        int px = 0;
        for (int i = 0; i < len; i++) {
            int idx = s[i] - 'a';
            if(p->next[idx]->endd) num[px++] = i;
            p = p->next[idx];
        }
        for (int i = 0; i < px; i++) {
            p = root;
            int j = num[i] + 1;
            for (; j < len; j++) {
                int idx = s[j] - 'a';
                if(p->next[idx] == NULL) break;
                p = p->next[idx];
                if(j == len - 1 && p->endd) return true;
            }
        }
        return false;
    }
    int main() {
        build_trie(); int t = 0;
        while (scanf("%s", str[t]) != EOF) {insert_trie(str[t]); t++;}
        for (int i = 0; i < t; i++) {
            if (query_trie(str[i])) printf("%s
    ", str[i]);
        }
        return 0;
    }



  • 相关阅读:
    POJ--1056 IMMEDIATE DECODABILITY && POJ--3630 Phone List(字典树)
    DHU--1247 Hat’s Words && HiHocder--1014 Trie树 (字典树模版题)
    HDU--5519 Sequence II (主席树)
    POJ--2104 K-th Number (主席树模版题)
    校内选拔赛题解
    HDU--4417 Super Mario (主席树模版题)
    ACM-ICPC 2017 Asia Xi'an A XOR (线性基+线段树思想)
    模版-树链剖分
    Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度
    Codeforces Gym 100431D Bubble Sort 水题乱搞
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770750.html
Copyright © 2011-2022 走看看