hdu5384
题意:给你n个母串。m个匹配串,让你求出对于每一个母串 全部匹配串出现的次数和。
思路:完全然全邝斌的模板啊。。。 凝视掉一行代码就能a。。。
。
代码:
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define sss(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a) memset(a,0,sizeof(a))
#define ss(a,b) scanf("%d%d",&a,&b)
#define s(a) scanf("%d",&a)
#define p(a) printf("%d\n", a)
#define INF 0x3f3f3f3f
#define w(a) while(a)
#define PI acos(-1.0)
#define LL long long
#define eps 10E-9
#define N 100010
#define mod 258280327
const int SIGMA_SIZE=26;
const int MAXN=100010;
const int MAXNODE=600010;
using namespace std;
void mys(int& res)
{
int flag=0;
char ch;
while(!(((ch=getchar())>='0'&&ch<='9')||ch=='-'))
if(ch==EOF) res=INF;
if(ch=='-') flag=1;
else if(ch>='0'&&ch<='9') res=ch-'0';
while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0';
res=flag?-res:res;
}
void myp(int a)
{
if(a>9)
myp(a/10);
putchar(a%10+'0');
}
/*************************THE END OF TEMPLATE************************/
char str1[N][10001];
char s[N];
int m, ans;
struct ac_trie{
int next[500010][26], ed[500010], fail[500010];
int root, L;
int newnode(){
for(int i=0; i<26; i++) next[L][i] = -1;
ed[L++] = 0;
return L-1;
}
void init(){
L = 0;
root = newnode();
}
void m_insert(char *s){
int len = strlen(s);
int now = root;
for(int i = 0; i<len; i++){
if(next[now][s[i]-'a'] == -1) next[now][s[i]-'a'] = newnode();
now = next[now][s[i]-'a'];
}
ed[now]++;
}
void getfail(){
queue<int>Q;
fail[root] = root;
for(int i=0; i<26; i++)
if(next[root][i] == -1) next[root][i] = root;
else{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
w(!Q.empty()){
int now = Q.front();
Q.pop();
for(int i=0; i<26; i++){
if(next[now][i] == -1) next[now][i] = next[fail[now]][i];
else{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
int query(char *s){
int len = strlen(s);
int now = root;
int res = 0;
for(int i=0; i<len; i++){
now = next[now][s[i]-'a'];
int temp = now;
w(temp!=root){
res += ed[temp];
//ed[temp] = 0;
temp = fail[temp];
}
}
return res;
}
};
ac_trie ac;
int main(){
int t, n;
s(t);
w(t--){
ss(n, m);
ac.init();
for(int i=0; i<n; i++) scanf("%s",str1[i]);
for(int i=0; i<m; i++){
scanf("%s", s);
ac.m_insert(s);
}
ac.getfail();
for(int i=0; i<n; i++) p(ac.query(str1[i]));
}
return 0;
}
hdu5384的更多相关文章
- [2015hdu多校联赛补题]hdu5384 Danganronpa
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:函数f(A, B)定义:A.B为字符串,f(A, B)为A中有多少个不同的B(ex:f(& ...
- hdu5384(trie树)
hdu5384 给定n个字符串Ai 给定m个字符串Bi 问所有的Bi在每个Ai中出现了多少次 很显然,对Bi建Trie图,然后每次用Ai去匹配的时候,不断查找当前匹配串的最长后缀,这样就能找到答案了 ...
- hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数
http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franc ...
- 2015 多校联赛 ——HDU5384(AC自动机)
Sample Input 1 5 6 orz sto kirigiri danganronpa ooooo o kyouko dangan ronpa ooooo ooooo Sample Out ...
- HDU-5384
Danganronpa Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- AC自动机(模板+例题)
首先要明白AC自动机是干什么的: AC自动机其实就是一种多模匹配算法,那么你可能会问什么叫做多模匹配算法.下面是我对多模匹配的理解,与多模与之对于的是单模,单模就是给你一个单词,然后给你一个字符串,问 ...
随机推荐
- 使用spyder3调试python程序
Ctrl+F5 以Debug模式运行文件 在debug之前记得用%reset 指令清空一下ipython工作空间中的变量,以免影响debug中变量值的查看 无论你是否打断点,都会在第一行语句执行之前中 ...
- 元类相关(type & metaclass)
metaclass作用: 1) 拦截类的创建 2) 修改类 3) 返回修改之后的类 """为什么要用metaclass类而不是函数? 由于__metaclass__可以接 ...
- Python学习-day8 socket进阶
还是继续socket网络编程的学习. socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) Socket Fami ...
- set(集合)类
1.set(集合)和 map(映射) 都属于关联容器,它们都支持查询一个元素是否存在,并能有效地获取元素.实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的 ...
- How to use MJRefresh
Installation with CocoaPods:pod 'MJRefresh' Manual import: Drag All files in the MJRefresh folder to ...
- hdoj--1010<dfs+奇偶剪枝>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...
- 校赛——1096Is The Same?(KMP或字符串的最小、大表示法)
1096: Is The Same? Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 26 Solved: 8[Submit][Status][Web B ...
- bzoj 1572: [Usaco2009 Open]工作安排Job
Description Farmer John 有太多的工作要做啊!!!!!!!!为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. 他的工作日从0时刻开始,有1000000000个单 ...
- 刷题总结——生产产品(vijo1243)
题目: 描述 在经过一段时间的经营后,dd_engi的OI商店不满足于从别的供货商那里购买产品放上货架,而要开始自己生产产品了!产品的生产需要M个步骤,每一个步骤都可以在N台机器中的任何一台完成,但生 ...
- php的异常处理
https://my.oschina.net/sallency/blog/837615