首页 技术 正文
技术 2022年11月8日
0 收藏 725 点赞 2,088 浏览 1304 个字

听说这题不公开.. 那就不贴题意了

一眼看上去还以为是exkmp的裸题.. 看了数据范围,呵呵..

多串匹配嘛.. 就用AC自动机咯,而且每个点最多也就只有$4$个孩子

用原串在AC自动机上走,碰到的和fail到的都是可以到达的字符串,每个点标记记录一下,这个时间复杂度是$O(n)$的

然后再每个串走AC自动机,找到最远的被标记的点就是答案了..

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
const int Maxn = ;
const int Maxm = ;
struct trie {
int child[], fl, fail;
}tr[Maxm]; int tot, rt;
char s[Maxm], st[Maxn][]; int n, stl[Maxn];
int m;
int get ( char c ){
if ( c == 'N' ) return ;
if ( c == 'W' ) return ;
if ( c == 'E' ) return ;
return ;
}
void bulid ( int &now, int l, int x ){
if ( !now ) now = ++tot;
if ( l == stl[x]+ ) return;
bulid ( tr[now].child[get(st[x][l])], l+, x );
}
void bulid_ac (){
queue <int> q;
q.push (rt);
while ( !q.empty () ){
int x = q.front (); q.pop ();
for ( int i = ; i < ; i ++ ){
int y = tr[x].child[i];
if ( !y ){
if ( x == rt ) tr[x].child[i] = x;
else tr[x].child[i] = tr[tr[x].fail].child[i];
}
else {
if ( x == rt ) tr[y].fail = x;
else tr[y].fail = tr[tr[x].fail].child[i];
}
if ( y > ) q.push (y);
}
}
}
int find ( int now, int l, int x ){
if ( l == stl[x]+ ) return stl[x];
if ( tr[tr[now].child[get(st[x][l])]].fl == ) return l-;
return find ( tr[now].child[get(st[x][l])], l+, x );
}
int main (){
int i, j, k;
scanf ( "%d%d", &n, &m );
scanf ( "%s", s+ );
for ( i = ; i <= m; i ++ ){
scanf ( "%s", st[i]+ );
stl[i] = strlen (st[i]+);
bulid ( rt, , i );
}
bulid_ac ();
int now = rt;
for ( i = ; i <= n; i ++ ){
now = tr[now].child[get(s[i])];
int x = now;
while ( tr[x].fl == && x ){
tr[x].fl = ;
x = tr[x].fail;
}
}
for ( i = ; i <= m; i ++ ){
printf ( "%d\n", find ( rt, , i ) );
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,495
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,741
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298