【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 ...
随机推荐
- lvs+keepalive主从和主主架构
下面配置主从 1)关闭SELinux和防火墙 vi /etc/sysconfig/selinux SELINUX=disabled setenforce 临时关闭SELinux,文件配置后,重启生效 ...
- UITableView滑动动画+FPSLabel
主要使用了tableView的代理方法 行将要显示的时候 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableView ...
- tyvj 1432 楼兰图腾
树状数组 本题数据有误 对于每一个点用权值树状数组维护在这个点之后之前的比他大和比他小的数 #include <iostream> #include <cstdio> #inc ...
- Cucumber Vs RobotFramework
I asked this same question a little over a year ago on this list when we were at your stage. Before ...
- Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp
D - MUH and Cube Walls Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & % ...
- 标准C程序设计七---05
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- msp430项目编程44
msp430综合项目---门禁控制系统44 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结
- Maven单元测试
// SKU码:系列前3位+6位年月日+3位序号(自动生产,取数据库中当天最大的,没有就赋值位001) // 订单编号:BRD+6位年月日+5位序号 // // 退单号:BRT+6位年月日+3位序号 ...
- 邁向IT專家成功之路的三十則鐵律 鐵律一:IT人生存之道-柔
老子在道德經裡頭曾提到:「天下之至柔,馳聘天下之至堅」,又說:「堅強者死之徒,柔弱者生之徒」.其實人在面對世間的萬事萬物都是一樣的,只是當我們學習將這個至理套用在IT的工作職場時,將可以讓我們在這條崎 ...
- Blazor——Asp.net core的新前端框架
原文:Blazor--Asp.net core的新前端框架 Blazor是微软在Asp.net core 3.0中推出的一个前端MVVM模型,它可以利用Razor页面引擎和C#作为脚本语言来构建WEB ...