【HDU 5384】Danganronpa(AC自己主动机)
看官方题解貌似就是个自己主动机裸题
比赛的时候用kuangbin的AC自己主动机模板瞎搞的,居然A了,并且跑的还不慢。。
存下模板吧
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 500010;
const int maxd = 26;
struct Trie{
int next[maxn][maxd],fail[maxn],end[maxn];
int root,L;
int num[101000];
int newnode(){
for(int i = 0;i < maxd;i++)
next[L][i] = -1;
end[L++] = 0;
return L - 1;
}
void init(){
L = 0;
root = newnode();
memset(num,0,sizeof(num));
}
void 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'];
}
end[now] ++;
}
void build(){
queue<int>Q;
fail[root] = root;
for(int i = 0;i < maxd;i++)
if(next[root][i] == -1)
next[root][i] = root;
else{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while(!Q.empty()){
int now = Q.front();
Q.pop();
for(int i = 0;i < maxd;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]);
}
}
}
void query(string &buf,int id){
int len = buf.size();
int now = root;
for(int i = 0;i < len; i++){
now = next[now][buf[i] - 'a'];
int temp = now;
while(temp != root){
num[id] += end[temp];
temp = fail[temp];
}
}
}
};
Trie ac;
char cstr[maxn];
vector<string>_find;
string _str;
int main(){
int n,m;
int T;
scanf("%d",&T);
while(T--){
ac.init();
scanf("%d%d",&n,&m);
_find.clear();
for(int i = 1; i <= n; i++){
cin >> _str;
_find.push_back(_str);
}
for(int i = 1; i <= m; i++){
scanf("%s",cstr);
ac.insert(cstr);
}
ac.build();
for(int i = 0; i < n; i++)
ac.query(_find[i],i);
for(int i = 0; i < n; i++)
printf("%d\n",ac.num[i]);
}
return 0;
}
【HDU 5384】Danganronpa(AC自己主动机)的更多相关文章
- HDOJ 5384 Danganronpa AC自己主动机
AC自己主动机裸题 Danganronpa Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java ...
- Hdu 5384 Danganronpa (AC自动机模板)
题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222 Keywords ...
- Hdu 3962 Microgene (AC自己主动机+矩阵)
标题效果: 构造一个字符串,使得有两个和两个以上的目标串.长短L这一系列有多少串都. IDEAS: 只有全款减有1一些字符串,没有目标就是答案. 假定数据是非常小的,够用dp解.dp[i][j][k] ...
- HDU 5384 Danganronpa (AC自己主动机模板题)
题意:给出n个文本和m个模板.求每一个文本中全部模板出现的总次数. 思路:Trie树权值记录每一个模板的个数.对于每一个文本跑一边find就可以. #include<cstdio> #in ...
- POJ 3691 & HDU 2457 DNA repair (AC自己主动机,DP)
http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...
- hdu 4057 AC自己主动机+状态压缩dp
http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...
- HDU 2825 Wireless Password (AC自己主动机,DP)
pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...
- HDU 2896 病毒侵袭 (AC自己主动机)
pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
随机推荐
- Leetcode 357.计算各个位数不同的数字个数
计算各个位数不同的数字个数 给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n . 示例: 输入: 2 输出: 91 解释: 答案应为除去 11,22,33 ...
- windows和ubuntu14.04双系统设置默认启动项
首先开机或者重启,在启动项选择菜单处记住win7对应的序号,从上至下的序号从0开始计数,我的win7系统选项处于第5个,那么序号就应该是4,记住后,打开ubuntu系统. 2 按下Ctrl+alt+T ...
- 九度oj 题目1135:字符串排序
题目描述: 先输入你要输入的字符串的个数.然后换行输入该组字符串.每个字符串以回车结束,每个字符串少于一百个字符. 如果在输入过程中输入的一个字符串为“stop”,也结束输入. 然后将这输入的该组字符 ...
- 原生 ajax get请求数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【bzoj1174】[Balkan2007]Toponyms Trie树
题目描述 给你一个字符集合,你从其中找出一些字符串出来. 希望你找出来的这些字符串的最长公共前缀*字符串的总个数最大化. 输入 第一行给出数字N.N在[2,1000000] 下面N行描述这些字符串,长 ...
- 虚拟机安装centos6.5
最近想搞一下代码覆盖率的jacoco,需要在linux环境下部署一套jenkins.故需要装一个centos的虚拟机. 一.安装虚拟机. 下载后安装一个虚拟机,我选择的是VMware虚拟机 二.安装c ...
- [kubernetes] 使用 Minikube 快速搭建本地 k8s 环境 (基于 Docker 驱动模式)
一.实验环境 操作系统:Centos 7 x86_64 Docker:1.12.6 二.部署 k8s 步骤 2.1 安装 kubectl cat <<EOF > /etc/yum. ...
- P3799 妖梦拼木棒 (组合数学)
题目背景 上道题中,妖梦斩了一地的木棒,现在她想要将木棒拼起来. 题目描述 有n根木棒,现在从中选4根,想要组成一个正三角形,问有几种选法? 输入输出格式 输入格式: 第一行一个整数n 第二行n个整数 ...
- OsCache MemCached EhCache
Memcache:分布式内存对象缓存系统,占用其他机子的内存.很多互联网,负载均衡三台(以三台为例)web服务器可以共享一台Memcache的资源.传递的信息以键值对的形式存储.传递的数据要实现序列化 ...
- UVa294 Divisors
在一段区间[l,r]内,找出因数最多的数的个数以及其因数个数. 用唯一分解定理将一个数分解成质因数的乘积,例如 2^p1*3^p2*5^p3*7^p4*.... 从这些质因数中任选出一些数相乘,都可 ...