有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程。
标签:process print not second #define scan fine NPU script
BZOJ_3942_[Usaco2015 Feb]Censoring_KMP
有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 1000050
int nxt[N],ls,lw,a[N],pos[N];
char s[N],w[N];
void get_nxt() {
int i,j=0;
for(i=2;i<=lw;i++) {
while(j&&w[j+1]!=w[i]) j=nxt[j];
nxt[i]=(w[j+1]==w[i])?++j:0;
}
}
void work() {
int i,j,top=0;
for(i=1;i<=ls;i++) {
j=pos[top];
a[++top]=s[i];
while(j&&w[j+1]!=s[i]) j=nxt[j];
if(w[j+1]==s[i]) j++;
if(j==lw) {
top-=lw;
}else pos[top]=j;
}
for(i=1;i<=top;i++) printf("%c",a[i]);
}
int main() {
scanf("%s%s",s+1,w+1);
ls=strlen(s+1); lw=strlen(w+1);
get_nxt();
work();
}
BZOJ_3942_[Usaco2015 Feb]Censoring_KMP
标签:process print not second #define scan fine NPU script
原文地址:https://www.cnblogs.com/suika/p/9062414.html