悬关求调

P8306 【模板】字典树

ln001 @ 2024-01-11 17:11:06

RT

提交

// Problem: P8306 【模板】字典树
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P8306
// Memory Limit: 1 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define ll long long
#define fst                      \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define f(x, y, z) for (register ll x = (y); x <= (z); x++)
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3f, N = 3e6 + 10;
ll T;
ll n, q;
namespace Trie
{
ll sz = 0, S[N];
unordered_map<char, ll> M[N];
void insert(string &s)
{
    ll p = 0;
    for (auto i : s)
    {
        if (M[p][i])
        {
            //S[p]++;
            p = M[p][i];
        }
        else
        {
            p = M[p][i] = ++sz;
            //S[p]++;
        }
        S[p]++;
    }
}
ll query(string &s)
{
    ll p = 0;
    for (auto i : s)
    {
        if (M[p][i])
        {
            S[p]++;
            p = M[p][i];
        }
        else
        {
            return 0;
        }
    }
    return S[p];
}
void clear()
{
    f(i, 0, sz + 1) //
    {
        M[i].clear();
        S[i] = 0;
    }
    sz = 0;
}
} // namespace Trie
string S;
signed main()
{
    fst;
    cin >> T;
    while (T--)
    {
        cin >> n >> q;
        f(i, 1, n)
        {
            cin >> S;
            Trie::insert(S);
        }
        f(i, 1, q)
        {
            cin >> S;
            cout << Trie::query(S) << endl;
        }
        Trie::clear();
    }
    return 0;
}

by Shen_Linwood @ 2024-01-11 20:47:01

@ln001 我拿自己的代码跑了波对拍,找到了一组 hack 数据:

1
12 10
eddadbedaadebaac
aecaaeabecebe
dabacbacbc
aacedabebebbabbcaeeebadacd
cccaabcabbaaeeedabeddadadeab
eabdaaadecbde
aaedacbaddaaeaeb
deacaadcccbaeabaaebabcccdbebde
cdaebecaaeabcaadcaeacab
eaaecdbccbacbaaabaabceacdc
aaabcaacebcacaddcacabbaaa
dbbaddababbabab
caadabcaa
caecaea
aaabccaaa
eaa
aaa
adccbedd
ebe
abbed
aaeaacadd
aca

您的输出:

0
0
0
1
2
0
0
0
0
0

我的输出:

0
0
0
1
1
0
0
0
0
0

by Shen_Linwood @ 2024-01-11 20:48:49

另外,如果只询问您出错的第 5 组询问,您的答案是正确的


by Shen_Linwood @ 2024-01-11 20:49:51

您可以自己对着调一下,我要跑路去做 whk 作业了(
明天如果我有空再来看看


by Shen_Linwood @ 2024-01-11 20:51:24

另外,如果只询问您出错的第 5 组询问,您的答案是正确的

这说明出错的地方很可能是在询问是修改了某些东西


by ln001 @ 2024-01-12 13:10:02

@Shen_Linwood 感谢大佬,A了,我再query函数脑抽写了一句S[p]++;

已关注


|