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;
}



原文地址:https://www.cnblogs.com/cniwoq/p/6770750.html