【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 ...
随机推荐
- [android 开发篇] 易白教程网址
http://www.yiibai.com/android/android_bluetooth.html
- python刷toj
1452 import sys a , b , c = map(int,sys.stdin.readline().split()) print ((a+b+c),(a*b*c), '%.2f' %(( ...
- Quartz与Terracotta的集成
Quartz与Terracotta的集成 博客分类: Java&Socket 企业应用面临的问题 开源组件的应用 quartz任务集群数据库任务调度Terracotta Quartz与Ter ...
- HDU——2612Find a way(多起点多终点BFS)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- VS链接错误: LNIK1123
问题:编译一个VS工程程序,出现连接错误:"LNK1123: 转换到 COFF 期间失败: 文件无效或损坏" 原因分析:连接器LNK是通过调用cvtres.exe完成文件向coff ...
- day1之校花网小试牛刀
一 利用生成器来完成爬去校花网视频 import requests import re import os import hashlib import time DOWLOAD_PATH=r'D:\D ...
- Nearest Common Ancestors(poj 1330)
题意:给定一棵树,询问两个节点的最近公共祖先. 输入:第一行T,表示测试组数. 每组测试数据包含一个n,表示节点数目,下面n-1行是连接的边,最后一行是询问 输出:共T行,代表每组的测试结果 /* 倍 ...
- localStorage增删改查
/** * 设置 本地缓存 */ export function setStorage(key, obj) { if (typeof obj === 'string') { localStorage. ...
- msp430项目编程40
msp430综合项目---多路温度检测系统40
- substring用法
字符串截取,substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串. substring(int beginIndex, int endIndex) 返回一 ...