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自动机其实就是一种多模匹配算法,那么你可能会问什么叫做多模匹配算法.下面是我对多模匹配的理解,与多模与之对于的是单模,单模就是给你一个单词,然后给你一个字符串,问 ...
随机推荐
- Linux的档案权限与目录配置
重点回顾:1.Linux的每个档案中,依据权限分为使用者.群组与其他人三种身份 2.群组最有用的功能之一,就是当你在团队开发资源的时候,且每个账号都可以有多个群组的支持 3.利用"ls -l ...
- 树状数组 - BZOJ 1103 [POI2007]大都市
bzoj 1103 [POI2007]大都市 描述 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员 Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景. ...
- oracle 多表连接查询
一.内连接(inner join (可简写为join)) 内连接查询操作列出与连接条件匹配的数据行,它使用比较运算符比较被连接列的列值. 1.等值连接:在连接条件中使用等于号(=)运算符比较被连接列的 ...
- day01_02.php的开发环境准备
PHP开发环境的准备 此套课程推荐xampp,也就是Apache+Mysql+PHP 但是我自己的机器装的是wamp环境,稍微有一些不一样,但是不影响使用
- 【水】ZYH记
我从十二岁起,便在sdoj的蒟蒻餐厅里当伙计,root说,样子太傻,怕侍候不了专职切题,就在外面做点事罢.外面的调试管理,虽然容易说话,但唠唠叨叨缠夹不清的也很不少.他们往往要亲眼看着一个字一个字编译 ...
- Shape,expand_dims,slice基本用法
import tensorflow as tf t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], ...
- python3--类与继承和组合
类和继承:“是一个”关系 我们已经深入探索了继承的机制,这里举个例子来说明它是如何用于模拟真实世界的关系的.从程序员的角度来看,继承是由属性点号运算启动的,由此触发实例.类以及任何超类中的变最名搜索. ...
- POJ 1240 Pre-Post-erous!
k叉树的前序和后续遍历,问一共有多少种这样的k叉树 这个就是树的同构,组合数就能解决 同样的题目在51nod也有的,我的另一篇博客 POJ 1240 Pre-Post-erous! We are al ...
- 【图文】 使用ant编译和发布java项目
开发JavaEE项目经常会碰到修改代码后,项目没有重新编译的问题.老大给指明了一个解决办法:用ant编译项目. ant是apache基金会下的一个项目,是基于Java语言的构建工具. ...
- ubuntu 安装tomcat<服务器>
一.下载tomcat 可以先下载到本地,然后ftp到服务器 官方 Apache Tomcat 的下载页面(下面的链接是apache自己的镜像服务器的地址,不同网络连接的话,apache会给出不同的镜像 ...