UESTC807 - Caruta

Trie

记录结点数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define MOD ((int)(1e9) + 7)
#define eps 1e-10
using namespace std;
typedef long long ll;
#define sigma_size 110
#define maxnode 100010
int res;
struct Trie {
    struct Node {
        int ch[sigma_size], value, cnt;
        void init() {
            memset(ch, 0, sizeof(ch));
            value = 0;
            cnt = 0;
        }
    };
    Node node[maxnode];
    int used, root;
    int P(char ch) {
        if ('0' <= ch && ch <= '9') return ch - '0';
        if ('a' <= ch && ch <= 'z') return 10 + ch - 'a';
        if ('A' <= ch && ch <= 'Z') return 36 + ch - 'A';
        return 62;
    }
    int newNode() {
        node[used].init();
        return used ++;
    }
    void init() {
        used = 0;
        root = newNode();
    }
    void Add(string &s, int v) {
        int x = root;
        for (int i = 0; i < s.size(); ++ i) {
            int p = P(s[i]);
            if (!node[x].ch[p]) node[x].ch[p] = newNode();
            node[x].cnt ++;
            x = node[x].ch[p];
        }
        node[x].value = v;
    }
    void Query(string &s) {
        int x = root;
        for (int i = 0; i < s.size(); ++ i) {
            int p = P(s[i]);
            node[x].cnt --;
            if (node[x].cnt == 0) return;
            res ++;
            x = node[x].ch[p];
        }
    }
}tx;
string str[100010];
int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        tx.init();
        res = 0;
        for (int i = 0; i < n; ++ i) {
            cin >> str[i];
            tx.Add(str[i], -1);
        }
        for (int i = 0; i < n-1; ++ i) {
            tx.Query(str[i]);
        }
        printf("%.6lf\n",(double)res);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值