码迷,mamicode.com
首页 > 其他好文 > 详细

Censoring

时间:2019-08-24 23:07:06      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:自动机   const   scanf   有一个   匹配   stream   一个栈   添加   strlen   

USACO15FEB-Censoring

题意:

有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程。

解法:

AC自动机在预处理fail的时候, $ O(n) $ 的预处理下节点对应的匹配串的长度。
再开一个栈,在 $ fail$ 图上跑一下就行了。
总的来说依然是一道AC自动机的板子题。

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std; 

const int N = 1000100; 
char s2[N], s1[N], s[N]; 
int nxt[N], sta[N], len, lenp; 

void getnext() {
    int i = 0, j = -1; 
    nxt[i] = j; 
    for (int i = 1 ; i < len ; i++) {
        while (j != -1 && s2[i] != s2[j + 1])j = nxt[j]; 
        if (s2[i] == s2[j + 1])++j; 
        nxt[i] = j; 
    }
}

void solve() {
    int top = 0; 
    for (int i = 0; i < len; i++) {
        int j = sta[top]; 
        s[++top] = s1[i]; 
        while (j != -1 && s2[j + 1] != s[top])j = nxt[j]; 
        if (s2[j + 1] == s[top])++j; 
        sta[top] = j; 
        if (sta[top] == lenp - 1) 
            top -= lenp; 
    }
    s[top + 1] = '\0'; 
    puts(s + 1); 
}

int main() {
    scanf("%s%s", s1, s2); 
    len = strlen(s1), lenp = strlen(s2); 
    getnext(); 
    solve(); 
    return 0; 
}

Censoring

标签:自动机   const   scanf   有一个   匹配   stream   一个栈   添加   strlen   

原文地址:https://www.cnblogs.com/Repulser/p/11406228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!