【AC自动机】屏蔽词删除(ybtoj AC自动机-4)
生活随笔
收集整理的這篇文章主要介紹了
【AC自动机】屏蔽词删除(ybtoj AC自动机-4)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
正題
ybtoj AC自動機-4
題目大意
有一個字符串和若干要刪除的串(不存在包含關系),每次從前往后搜,搜到第一個要刪除的串然后刪掉,再從0開始搜
問你最后得到的字符串
解題思路
先把所有刪除串丟進AC自動機中,每個串的最后位置標注一下
然后在AC自動機中跑原串,每到一位丟進棧中,如果跑到一個刪除串的結尾,那么把棧中刪除的部分刪掉,然后從刪除串的前一位繼續搜
代碼
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define ll long long #define N 100100 using namespace std; int n, w, top, v[N], p[N], nx[N], to[N][30]; char s[N], ss[N], a[N]; queue<int>d; void insert(char* s) {int n = strlen(s+1), now = 0;for (int i = 1; i <= n; ++i){int y = s[i] - 'a';if (!to[now][y]) to[now][y] = ++w;now = to[now][y];}p[now] = n;return; } void bfs() {for (int i = 0; i < 26; ++i)if (to[0][i]) d.push(to[0][i]);while(!d.empty()){int h = d.front();d.pop();for (int i = 0; i < 26; ++i)if (!to[h][i]) to[h][i] = to[nx[h]][i];else nx[to[h][i]] = to[nx[h]][i], d.push(to[h][i]);}return; } void solve(char* s) {int n = strlen(s+1), now = 0;for (int i = 1; i <= n; ++i){int y = s[i] - 'a';now = to[now][y];a[++top] = s[i];//丟進棧中v[top] = now;//記錄位置top -= p[now];//跑到刪除串就刪掉now = v[top];}return; } int main() {scanf("%s", ss+1);scanf("%d", &n);for (int i = 1; i <= n; ++i){scanf("%s", s+1);insert(s);}bfs();solve(ss);for (int i = 1; i <= top; ++i)putchar(a[i]);return 0; }總結
以上是生活随笔為你收集整理的【AC自动机】屏蔽词删除(ybtoj AC自动机-4)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 隐藏电脑文件如何设置电脑的隐藏
- 下一篇: windows系统更新怎么关闭电脑更新时