hdu 1247 Hat’s Words(字典树)

题目链接:hdu 1247 Hat’s Words

题目大意:给定若干个字符串,判断是否存在一个字符串是另外两个非空串拼接而成。按照字典序输出所有满足字符

串。

解题思路:对所有串建立字典树,然后判断每一个串,对于单个字符串,枚举分割点,分别在字典树中查找两个字符串

是否存在。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 50005;
const int maxm = 105;
const int sigma_size = 26;

struct Tire {
    int sz;
    int g[maxn * maxm][sigma_size];
    int val[maxn * maxm];

    void init();
    int idx(char ch);
    void insert(char* s);
    int find(char* s);
}S;

char s[maxn][maxm], w[maxm];

int main () {
    S.init();
    int n = 0;

    while (scanf("%s", w) == 1) {
        strcpy(s[n++], w);
        S.insert(w);
    }

    for (int i = 0; i < n; i++) {
        int len = strlen(s[i]);
        for (int j = 1; j < len - 1; j++) {
            strncpy(w, s[i], j);
            w[j] = '\0';
            if (S.find(w) && S.find(s[i]+j)) {
                printf("%s\n", s[i]);
                break;
            }
        }
    }
    return 0;
}

void Tire::init() {
    sz = 1;
    val[0] = 0;
    memset(g[0], 0, sizeof(g[0]));
}

int Tire::idx (char ch) {
    return ch - 'a';
}

int Tire::find(char* s) {
    int u = 0, n = strlen(s);

    for (int i = 0; i < n; i++) {
        int v = idx(s[i]);

        if (g[u][v] == 0)
            return 0;
        u = g[u][v];
    }
    return val[u];
}

void Tire::insert(char* s) {
    int u = 0, n = strlen(s);

    for (int i = 0; i < n; i++) {
        int v = idx(s[i]);

        if (g[u][v] == 0) {
            val[sz] = 0;
            memset(g[sz], 0, sizeof(g[sz]));
            g[u][v] = sz++;
        }

        u = g[u][v];
    }
    val[u] = 1;
}

你可能感兴趣的:(hdu 1247 Hat’s Words(字典树))