Trie(前缀树)

参考:《算法竞赛入门经典训练指南》





1、UVALive 3942 Remember the Word

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <ctime>
#include <cctype>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pair;

const int mod = 20071027;
const int INF = 0x7fffffff;
const int maxn = 4e5 + 10;

struct Trie {
    int son[maxn][30], val[maxn];
    int size;
    Trie() { size = 1, memset(son[0], 0, sizeof(son[0])); }
    int idx(char c) { return c - 'a'; }
    void insert(char *s, int _val);
};

char str[maxn], word[4010][110];
int S, Case = 0;
ll dp[maxn];
Trie trie;

int main()
{
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
#endif // __AiR_H_
    while (scanf("%s %d", str, &S) != EOF) {
        trie.size = 1;
        memset(trie.son[0], 0, sizeof(trie.son[0]));
        for (int i = 0; i < S; ++i) {
            scanf("%s", word[i]);
            trie.insert(word[i], 1);
        }
        int len = strlen(str);
        memset(dp, 0, sizeof(dp));
        dp[len] = 1;
        for (int i = len - 1; i >= 0; --i) {
            int u = 0;
            for (int j = i; j < len; ++j) {
                int c = trie.idx(str[j]);
                if (trie.son[u][c] == 0) {
                    break;
                }
                u = trie.son[u][c];
                if (trie.val[u]) {
                    dp[i] = (dp[i] + dp[j + 1]) % mod;
                }
            }
        }
        printf("Case %d: %lld\n", ++Case, dp[0]);
    }
    return 0;
}

void Trie::insert(char *s, int _val)
{
    int u = 0, len = strlen(s);
    for (int i = 0; i < len; ++i) {
        int c = idx(s[i]);
        if (!son[u][c]) {
            memset(son[size], 0, sizeof(son[size]));
            val[size] = 0, son[u][c] = size++;
        }
        u = son[u][c];
    }
    val[u] = _val;
}


2、UVa 11732 "strcmp()" Anyone?

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <ctime>
#include <cctype>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pair;

const int mod = 20071027;
const int INF = 0x7fffffff;
const int maxn = 4e6 + 10;

struct Trie {
    char son[maxn];
    int tot[maxn], head[maxn], Next[maxn];
    int size;
    void clear() { size = 1, son[0] = tot[0] = head[0] = Next[0] = 0; }
    void insert(char *s);
    void dfs(int u, int depth);
};

int N, Case = 0;
Trie trie;
char word[4010];
ll ans = 0;

void dfs(int u, int depth);

int main()
{
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
#endif // __AiR_H_
    while (scanf("%d", &N) != EOF && N != 0) {
        trie.clear();
        ans = 0;
        for (int i = 0; i < N; ++i) {
            scanf("%s", word);
            trie.insert(word);
        }
        trie.dfs(0, 0);
        printf("Case %d: %lld\n", ++Case, ans);
    }
    return 0;
}

void Trie::dfs(int u, int depth)
{
    if (head[u] == 0) {
        ans += (ll)tot[u] * (tot[u] - 1) * depth; //2 * depth * (tot[u] - 1 + 1) * (tot[u] - 1) / 2
    } else {
        ll sum = 0;
        for (int v = head[u]; v != 0; v = Next[v]) {
            sum += (ll)tot[v] * (tot[u] - tot[v]);
        }
        ans += sum / 2 * (2 * depth + 1);
        for (int v = head[u]; v != 0; v = Next[v]) {
            dfs(v, depth + 1);
        }
    }
}

void Trie::insert(char *s)
{
    int u = 0, len = strlen(s), v;
    ++tot[0];
    for (int i = 0; i <= len; ++i) {
        bool found = false;
        for (v = head[u]; v != 0; v = Next[v]) {
            if (son[v] == s[i]) {
                found = true;
                break;
            }
        }
        if (!found) {
            v = size++, Next[v] = head[u], head[u] = v;
            head[v] = 0, son[v] = s[i], tot[v] = 0;
        }
        u = v;
        ++tot[u];
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值