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

一眼看上去还以为是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 ;
}

bzoj 4327: JSOI2012 玄武密码的更多相关文章

  1. BZOJ 4327: JSOI2012 玄武密码 后缀自动机

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...

  2. BZOJ 4327 JSOI2012 玄武密码(后缀自动机)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4327 [题目大意] 求每个子串在母串中的最长匹配 [题解] 对母串建立后缀自动机,用每 ...

  3. BZOJ 4327 [JSOI2012]玄武密码 (AC自动机)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=4327 题解: 做法挺显然,建出AC自动机之后在上面跑,标记所有走过的点,然后再进行递推 ...

  4. 【题解】bzoj 4327 JSOI2012 玄武密码

    原题传送门 我们先对所有询问串建立AC自动机(今天洛咕上有人分不清AC自动机和自动AC机) 然后将母串在AC自动机上跑,每走到一个点x,从x点出发沿着fail指针所能到的所有前缀都是匹配成功的,暴力向 ...

  5. 4327: JSOI2012 玄武密码

    4327: JSOI2012 玄武密码 Description 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老 ...

  6. 4327: JSOI2012 玄武密码[SAM]

    4327: JSOI2012 玄武密码 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 263  Solved: 112[Submit][Status] ...

  7. 【BZOJ4327】JSOI2012 玄武密码 AC自动机

    [BZOJ4327]JSOI2012 玄武密码 Description 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香 ...

  8. P5231 [JSOI2012]玄武密码

    P5231 [JSOI2012]玄武密码 链接 分析: 首先对所有询问串建立AC自动机,然后扫描一遍母串,在AC自动机上走,没走到一个点,标记这个点走过了,并且它的fail树上的祖先节点也可以访问到( ...

  9. 2021.11.10 P5231 [JSOI2012]玄武密码(AC自动机)

    2021.11.10 P5231 [JSOI2012]玄武密码(AC自动机) https://www.luogu.com.cn/problem/P5231 题意: 给出字符串S和若干T,求S与每个T的 ...

随机推荐

  1. Orchard 模块开发学习笔记 (1)

    创建模块 首先,打开Bin目录下的Orchard.exe 等到出现orchard>后, 看看命令列表中是否存在 codegen module 如果不存在,则需要先执行:feature enabl ...

  2. java开发人员,最应该学习和熟练使用的工具类。google guava.(谷歌 瓜娃)

    学习参考文章: http://blog.csdn.net/wisgood/article/details/13297535 http://ifeve.com/google-guava/ http:// ...

  3. 分页查询和分页缓存查询,List<Map<String, Object>>遍历和Map遍历

    分页查询 String sql = "返回所有符合条件记录的待分页SQL语句"; int start = (page - 1) * limit + 1; int end = pag ...

  4. javascript学习笔记(2)————this

    //简单的学习JavaScript中this关键词 //this在于我简单的理解就是谁调用了当前方法(函数),this就指向谁 var a = 20; function fn1(){ this.a = ...

  5. Android Studio 导出jar包

    不像在Eclipse,可以直接导出jar包.AndroidStudio只可以生成aar包. 在网上看到许多朋友问怎么可以像Eclipse一样导出jar包,其实我们只要知道它的原理就可以了. 用jar命 ...

  6. Js计算当前日,当前周开始结束时间,当前月份,当前年份

    <script type="text/javascript"> //日期加上天数后的新日期. function GetDateStr(AddDayCount) { va ...

  7. MySql: log 位置

    mysql日志文件位置 登录mysql终端日志文件路径mysql> show variables like 'general_log_file';+------------------+---- ...

  8. 使用外部web组件-----easyUI、jQueryUI、Bootstrap、js正则表达式

    1.使用外部web组件,以Bootstrap为例 <head> <link rel='stylesheet'  href='bootstrap-3.3.0-dist/dist/css ...

  9. Android issues

    1. Android studio 2.0 Error:Exception in thread "main" java.lang.UnsupportedClassVersionEr ...

  10. xposed XDA记录

    [OFFICIAL] Xposed for Lollipop/Marshmallow [Android 5.0/5.1/6.0, v86, 2016/10/31] http://forum.xda-d ...