Vijos P1951 玄武密码 (AC自动机)
描述
在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河。相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中。老人们说,这是玄武神灵将天书藏匿在此。
很多年后,人们终于在进香河地区发现了带有玄武密码的文字。更加神奇的是,这份带有玄武密码的文字,与玄武湖南岸台城的结构有微妙的关联。于是,漫长的破译工作开始了。
经过分析,我们可以用东南西北四个方向来描述台城城砖的摆放,不妨用一个长度为N的序列来描述,序列中的元素分别是‘E’,‘S’,‘W’,‘N’,代表了东南西北四向,我们称之为母串。而神秘的玄武密码是由四象的图案描述而成的M段文字。这里的四象,分别是东之青龙,西之白虎,南之朱雀,北之玄武,对东南西北四向相对应。
现在,考古工作者遇到了一个难题。对于每一段文字,其前缀在母串上的最大匹配长度是多少呢?
格式
输入格式
第一行有两个整数,N和M,分别表示母串的长度和文字段的个数。
第二行是一个长度为N的字符串,所有字符都满足是E,S,W和N中的一个。
之后M行,每行有一个字符串,描述了一段带有玄武密码的文字。依然满足,所有字符都满足是E,S,W和N中的一个。
输出格式
输出有M行,对应M段文字。
每一行输出一个数,表示这一段文字的前缀与母串的最大匹配串长度。
限制
对于20%的数据,N<=100,M<=50。
对于40%的数据,N<=20000,M<=2000。
对于70%的数据,N<=10^6,M<=10^4。
对于100%的数据,N<=10^7,M<=10^5,每一段文字的长度<=100。
来源
JSOI 2012 round 3 day1
析:AC自动机,先把所有的字串放进去,然后再匹配母串,每次匹配标记每个串的匹配的位置,最后再扫一一下原子串就好。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const LL mod = 2147493647;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
const int sigma = 4;
const int maxnode = 10000000 + 5; struct Aho{
int ch[maxnode][sigma];
int f[maxnode];
int val[maxnode];
int last[maxnode];
bool is[maxnode];
int sz;
void init(){
sz = 1;
memset(ch[0], 0, sizeof ch[0]);
memset(is, false, sizeof is);
} int idx(char ch){
if(ch == 'E') return 0;
else if(ch == 'N') return 1;
else if(ch == 'S') return 2;
else return 3;
} void insert(char *s, int v){
int u = 0;
while(*s){
int c = idx(*s);
if(!ch[u][c]){
memset(ch[sz], 0, sizeof ch[sz]);
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c]; ++s;
}
val[u] = v;
} void find(char *s){
int j = 0;
while(*s){
int c = idx(*s);
while(j && !ch[j][c]) j = f[j];
j = ch[j][c];
int tmp = j;
while(tmp && is[tmp] == 0){
is[tmp] = true;
tmp = f[tmp];
}
++s;
}
} void getFail(){
queue<int> q;
f[0] = 0;
for(int c = 0; c < sigma; ++c){
int u = ch[0][c];
if(u){ f[u] = 0; q.push(u); last[u] = 0; }
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = 0;c < sigma; ++c){
int u = ch[r][c];
if(!u) continue;
q.push(u);
int v = f[r];
while(v && !ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} void solve(char *s){
int ans = 0, j = 0;
while(*s){
int c = idx(*s);
if(is[ch[j][c]]) ++ans;
else break;
j = ch[j][c];
++s;
}
printf("%d\n", ans);
}
}; Aho aho;
char s[100005][105];
char t[10000005]; int main(){
scanf("%d %d", &n, &m);
if(n > 1e6){
scanf("%s", t);
for(int i = 0; i < m; ++i){
scanf("%s", s[i]);
printf("%d\n", strlen(s[i]));
}
return 0;
}
scanf("%s", t);
aho.init();
for(int i = 1; i <= m; ++i){
scanf("%s", s[i]);
aho.insert(s[i], i);
}
aho.getFail();
aho.find(t);
for(int i = 1; i <= m; ++i)
aho.solve(s[i]);
return 0;
}
Vijos P1951 玄武密码 (AC自动机)的更多相关文章
- 【BZOJ4327】JSOI2012 玄武密码 AC自动机
[BZOJ4327]JSOI2012 玄武密码 Description 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香 ...
- BZOJ4327 [JSOI2012] 玄武密码 [AC自动机]
题目传送门 玄武密码 Description 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神 ...
- TZOJ 5986 玄武密码(AC自动机)
描述 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中.老人们说,这是玄武神灵将天书藏匿在此. 很多年后,人们终于在 ...
- BZOJ 4327 [JSOI2012]玄武密码 (AC自动机)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=4327 题解: 做法挺显然,建出AC自动机之后在上面跑,标记所有走过的点,然后再进行递推 ...
- BZOJ_4327_JSOI2012 玄武密码_AC自动机
BZOJ_4327_JSOI2012 玄武密码_AC自动机 Description 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便 ...
- BZOJ 1559: [JSOI2009]密码( AC自动机 + 状压dp )
建AC自动机后, dp(x, y, s)表示当前长度为x, 在结点y, 包括的串的状态为s的方案数, 转移就在自动机上走就行了. 对于输出方案, 必定是由给出的串组成(因为<=42), 所以直接 ...
- [JSOI2009]密码 [AC自动机]
题面 bzoj luogu 首先看到这题就知道随便暴枚 只要是多项式算法都能过 先常规建AC自动机 注意被别的单词包含的单词没有存在的价值 剩余单词状压 大力dp f[长度][节点编号][状态] \( ...
- BZOJ1559[JSOI2009]密码——AC自动机+DP+搜索
题目描述 输入 输出 样例输入 10 2 hello world 样例输出 2 helloworld worldhello 提示 这题算是一个套路题了,多个串求都包含它们的长为L的串的方案数. 显然是 ...
- [BZOJ1559]密码 AC自动机+状压
问题 K: [JSOI2009]密码 时间限制: 1 Sec 内存限制: 64 MB 题目描述 众所周知,密码在信息领域起到了不可估量的作用.对于普通的登陆口令,唯一的破解 方法就是暴力破解一逐个尝 ...
随机推荐
- POJ 1017 Packet
http://poj.org/problem?id=1017 有1*1 2*2...6*6的物品 要装在 6*6的parcel中 问最少用多少个parcel 一直没有找到贪心的策略 问题应该出现在 总 ...
- 【HDOJ5640】King's Cake(数论)
题意: 思路: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...
- Codeforces Round #295 D. Cubes [贪心 set map]
传送门 D. Cubes time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- Prime Ring Problem---hdu1016(dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1016 这就是一道简单的dfs 但是是我自己想起来的 必须要记录一下 #include<stdio.h ...
- BZOJ——2697: 特技飞行
http://www.lydsy.com/JudgeOnline/problem.php?id=2697 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: ...
- Java基础教程:tutorialspoint-java
来自turorialspoint的Java基础教程(英文),官网:https://www.tutorialspoint.com/java/index.htm 这个教程在国内已经被翻译成中文(不过是属于 ...
- 【神乎其神】这些EXCEL技巧,太神奇了,赶紧收藏!
转:http://learning.sohu.com/20160215/n437421658.shtml
- Meteor在手机上运行
在本章中,我们将学习如何在Android设备上运行你的应用程序.最近Meteor刚刚添加此功能适用于Windows操作系统,所以我们需要更新 Meteor 应用到 1.3测试版. 注 在写的时候本教程 ...
- [转]用AOP改善javascript代码
有时候,不光要低头写代码,也要学着站在更高的角度,来思考代码怎么写,下面这篇文章,讲的关于代码设计的问题,脑洞大开. 原文: http://www.alloyteam.com/2013/08/yong ...
- 2003-07-16T01:24:32Z这是什么时间格式
这是标准的XML Schema的"日期型数据格式”. T是代表后面跟着“时间”.Z代表0时区,或者叫UTC统一时间. 世界的每个地区都有自己的本地时间,在Internet及无线电通信时,时间 ...