[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=4327

[算法]

AC自动机
[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e7 + ;
const int MAXM = 1e5 + ;
const int MAXLEN = ; int n,m;
int ans[MAXM];
char P[MAXN];
char s[MAXM][MAXLEN]; inline int get_value(char a)
{
if (a == 'E') return ;
if (a == 'S') return ;
if (a == 'W') return ;
if (a == 'N') return ;
}
struct AC_Automation
{
int tot;
int root;
struct Node
{
int child[];
int fail;
bool visited;
} trie[MAXN];
inline void insert(char *s)
{
int now = root;
int len = strlen(s + );
for (int i = ; i <= len; i++)
{
int val = get_value(s[i]);
if (!trie[now].child[val]) trie[now].child[val] = ++tot;
now = trie[now].child[val];
}
}
inline void rebuild()
{
int l,r;
static int q[MAXN];
q[l = r = ] = root;
trie[root].fail = -;
while (l <= r)
{
int cur = q[l];
l++;
for (int i = ; i < ; i++)
{
if (trie[cur].child[i])
{
if (cur == root)
trie[trie[cur].child[i]].fail = ;
else
{
int p = trie[cur].fail;
while (p != -)
{
if (trie[p].child[i])
{
trie[trie[cur].child[i]].fail = trie[p].child[i];
break;
} else p = trie[p].fail;
}
}
q[++r] = trie[cur].child[i];
}
}
}
}
inline void getans(char *s)
{
int now = root;
int len = strlen(s + );
for (int i = ; i <= len; i++)
{
int val = get_value(s[i]);
int p = now;
while (p != -)
{
if (trie[p].child[val]) break;
else p = trie[p].fail;
}
if (p == -)
{
now = ;
continue;
} else now = p = trie[p].child[val];
while (p)
{
if (!trie[p].visited)
{
trie[p].visited = true;
p = trie[p].fail;
} else break;
}
}
}
inline void calc(int pos,char *s)
{
int now = root;
int len = strlen(s + );
for (int i = ; i <= len; i++)
{
int val = get_value(s[i]);
now = trie[now].child[val];
if (trie[now].visited) ans[pos] = i;
}
}
} ACAM; int main()
{ scanf("%d%d",&n,&m);
scanf("%s",P + );
for (int i = ; i <= m; i++) scanf("%s",s[i] + );
for (int i = ; i <= m; i++) ACAM.insert(s[i]);
ACAM.rebuild();
ACAM.getans(P);
for (int i = ; i <= m; i++) ACAM.calc(i,s[i]);
for (int i = ; i <= m; i++) printf("%d\n",ans[i]); return ; }

[JSOI 2012] 玄武密码的更多相关文章

  1. Vijos P1951 玄武密码 (AC自动机)

    描述 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神灵将天书藏匿在此. 很多年后,人们终于在 ...

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

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

  3. 4327: JSOI2012 玄武密码

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

  4. BZOJ4327:[JSOI2012]玄武密码(SAM)

    Description 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神灵将天书藏匿在此.  ...

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

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

  6. P5231 [JSOI2012]玄武密码

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

  7. BZOJ4327 [JSOI2012] 玄武密码 [AC自动机]

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

  8. BZOJ-4327:JSOI2012 玄武密码(AC自动机模板题)

    在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神灵将天书藏匿在此.  很多年后,人们终于在进香 ...

  9. [BZOJ4327]:[JZOI2012]玄武密码(AC自动机)

    题目传送门 题目描述: 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神灵将天书藏匿在此.  ...

随机推荐

  1. 413 Request Entity Too Large报错处理

    修改nginx配置   这是最简单的一个做法,着报错原因是nginx不允许上传配置过大的文件,那么件把nginx的上传大小配置调高就好.    1.打开nginx主配置文件nginx.conf,一般在 ...

  2. 事件的节流(throttle)与防抖(debounce)

    事件的节流(throttle)与防抖(debounce) 有些浏览器事件可以在短时间内快速触发多次,比如调整窗口大小或向下滚动页面.例如,监听页面窗口滚动事件,并且用户持续快速地向下滚动页面,那么滚动 ...

  3. BLOCK层基本概念:bio,request,request_queue

    Summary bio 代表一个IO 请求 request 是bio 提交给IO调度器产生的数据,一个request 中放着顺序排列的bio 当设备提交bio 给IO调度器时,IO调度器可能会插入bi ...

  4. Linux学习笔记记录(五)

  5. python面向对象的特点,类定义等,私有属性、公有属性、成员属性

    引子:类的对象在内存中的表示def dog(name,dog_type): def bark(d): print(d,'wang wang wang ...') data = { 'name':nam ...

  6. 爬虫实战(一) 用Python爬取百度百科

    最近博主遇到这样一个需求:当用户输入一个词语时,返回这个词语的解释 我的第一个想法是做一个数据库,把常用的词语和词语的解释放到数据库里面,当用户查询时直接读取数据库结果 但是自己又没有心思做这样一个数 ...

  7. 2.5.4 华丽的 printf 输出

        如同echo命令,printf命令可以输出简单的字符串:         [many@avention my_sh]$ printf "Hello, world\n"    ...

  8. Spring常用注解总结 hibernate注解

    1.@Resource和@Autowired @Resource和@Autowired功能一样在容器查找匹配的Bean @Autowired默认按照byType方式进行bean匹配,@Resource ...

  9. uestc 1903

    #include<stdio.h> int main() { int n,m,i,t; scanf("%d",&t); while(t--){ scanf(&q ...

  10. java虚拟机(一)-java内存区域与内存溢出异常

    1.简述:java虚拟机在执行java程序的过程中,会把他所管理的内存分为以下几个区域, 1.1.程序计数器 1.2.虚拟机栈 1.3.本地方法栈 1.4.java堆 1.5.方法区 如图所示: 2. ...