萌新刚学 ACAM 样例不过 0pts 求条
查看原帖
萌新刚学 ACAM 样例不过 0pts 求条
1004065
xuduang楼主2024/8/7 16:04

rt,qwq

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
namespace AC{
int tr[N][30], fail[N], cnt[N], tot;
void insert(string s)
{
    int u = 0;
    for(int i = 1; s[i]; i++)
    {
        if(!tr[u][s[i] - 'a']) tr[u][s[i] - 'a'] = ++tot;
        u = tr[u][s[i] - 'a'];
    }
    cnt[u]++;
}
queue<int> p;
void build()
{
    for(int i = 0; i < 26; i++) if(tr[0][i]) p.push(tr[0][i]);
    while(!p.empty())
    {
        int u = p.front();
        p.pop();
        for(int i = 0; i < 26; i++)
        {
            if(tr[u][i])
            {
                fail[tr[u][i]] = tr[fail[u]][i];
                p.push(tr[u][i]);
            }
            else tr[u][i] = tr[fail[u]][i];
        }
    }
}
int query(string s)
{
    int u = 0, ans = 0;
    for(int i = 1; s[i]; i++)
    {
        u = tr[u][s[i] - 'a'];
        for(int j = u; j && ~cnt[j]; j = fail[j]) ans += cnt[j], cnt[j] = -1;
    }
    return ans;
}

}
using namespace AC;
char s[N];
int n;
signed main()
{
    cin >> n;
    for(int i = 1; i <= n; i++) scanf("%s", s + 1), insert(s);
    scanf("%s", s + 1);
    build();
    printf("%lld\n", query(s));
    return 0;
}
2024/8/7 16:04
加载中...