第一次打铁

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10005;
template<size_t N>
struct Trie
{
    struct Node
    {
        bool flag;
        unordered_map<string, Node*> next;
    } pool[N], *alloc, *root;
    Node* new_node(bool flag)
    {
        Node* res = alloc++;
        res->flag = flag;
        res->next.clear();
        return res;
    }
    void init()
    {
        alloc = pool;
        root = new_node(true);
    }
    void build(const vector<string>& v, bool flag)
    {
        Node* curr = root;
        for (auto& s : v)
        {
            if (curr->next.find(s) == curr->next.end())
                curr->next[s] = new_node(false);
            curr = curr->next[s];
            if (flag) curr->flag = true;
        }
    }
    int dfs(Node* x)
    {
        int res = 0;
        for (auto& i : x->next)
        {
            if (i.second->flag)
                res += dfs(i.second);
            else res++;
        }
        return res;
    }
};
Trie<MAXN> trie;
int n, m;
char line[MAXN];
int main()
{
//    freopen("input.in", "r", stdin);
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d", &n, &m);
//        printf("%d %d\n", n, m);
        trie.init();
        for (int i = 1; i <= n; i ++)
        {
            scanf("%s", line);
            vector<string> v;
            string s;
            for (int j = 0; line[j]; j ++)
            {
                char c = line[j];
                if (islower(c)) s += c;
                else
                {
                    v.push_back(s);
                    s = "";
                }
            }
            v.push_back(s);
            trie.build(v, false);
        }
        for (int i = 1; i <= m; i ++)
        {
            scanf("%s", line);
            vector<string> v;
            string s;
            for (int j = 0; line[j]; j ++)
            {
                char c = line[j];
                if (islower(c)) s += c;
                else
                {
                    v.push_back(s);
                    s = "";
                }
            }
            v.push_back(s);
            trie.build(v, true);
        }
        int ans = trie.dfs(trie.root);
        printf("%d\n", ans);
    }
    return 0;
}
/*
2
3 0
data/train
data/test
model

3 1
data/train
data/test
model
data/sample

*/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂的码泰君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值