看官方题解貌似就是个自己主动机裸题

比赛的时候用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自己主动机)的更多相关文章

  1. HDOJ 5384 Danganronpa AC自己主动机

     AC自己主动机裸题 Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java ...

  2. Hdu 5384 Danganronpa (AC自动机模板)

    题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222  Keywords ...

  3. Hdu 3962 Microgene (AC自己主动机+矩阵)

    标题效果: 构造一个字符串,使得有两个和两个以上的目标串.长短L这一系列有多少串都. IDEAS: 只有全款减有1一些字符串,没有目标就是答案. 假定数据是非常小的,够用dp解.dp[i][j][k] ...

  4. HDU 5384 Danganronpa (AC自己主动机模板题)

    题意:给出n个文本和m个模板.求每一个文本中全部模板出现的总次数. 思路:Trie树权值记录每一个模板的个数.对于每一个文本跑一边find就可以. #include<cstdio> #in ...

  5. POJ 3691 &amp; 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: ...

  6. hdu 4057 AC自己主动机+状态压缩dp

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...

  7. HDU 2825 Wireless Password (AC自己主动机,DP)

    pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...

  8. HDU 2896 病毒侵袭 (AC自己主动机)

    pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...

  9. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

随机推荐

  1. [uiautomator篇] 找父亲节点和其他兄弟节点

    https://testerhome.com/topics/1250 Appium [已解决] UiSelector 如何根据节点定位到父节点 / 兄弟节点? liqing380 · 发布于 2014 ...

  2. TOJ3039: 材质贴图

    3039: 材质贴图  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 46       ...

  3. lua-helloworld

    write script file, a.lua: #!/usr/bin/lua print("hello world!") add excutable prperty to th ...

  4. mysqli 封装

    <?php //header header('content-type:text/html;charset=UTF-8'); class DB { //定义属性 private $host;// ...

  5. Understanding performance, load and stress testing

    What are performance, load and stress testing? Performance testing, load testing and stress testing ...

  6. vue 使用Echarts 环形图 自定义legend formatter 富文本标签

    main.js 引入echarts // 引入echarts import Echarts from 'echarts' Vue.prototype.$echarts = Echarts   < ...

  7. iOS学习笔记13-网络(二)NSURLSession

    在2013年WWDC上苹果揭开了NSURLSession的面纱,将它作为NSURLConnection的继任者.现在使用最广泛的第三方网络框架:AFNetworking.SDWebImage等等都使用 ...

  8. yum update 出错解决办法

    卸载掉yum和python 于是我卸载了python和yum,觉得自己重新安装python和yum. 步骤1:卸载python rpm -qa|grep python|xargs rpm -e --a ...

  9. PHP文件上传设置和处理(多文件)

    <!--upload.php文件内容--><?phpheader("Content-Type:text/html;charset=utf-8");/* //原来$ ...

  10. Split The Tree

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB 题目描述 You are given a tree with n vertices, numbered from 1 ...