HDU 5384 Danganronpa (AC自己主动机模板题)
题意:给出n个文本和m个模板。求每一个文本中全部模板出现的总次数。
思路:Trie树权值记录每一个模板的个数。对于每一个文本跑一边find就可以。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<ctime>
#include<set>
#define eps 1e-6
#define LL long long
#define pii (pair<int, int>)
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; //const int INF = 0x3f3f3f3f;
int n, k, shit; const int maxn = 1000000 + 100;
const int SIGMA_SIZE = 26;
const int maxnode = 1000000+100;
int ch[maxnode][SIGMA_SIZE+5];
int val[maxnode], cnt[100000+1000];
int idx(char c) {return c - 'a';}
struct Trie {
int sz;
Trie() { sz = 1; memset(ch[0], 0, sizeof(ch[0]));};
void insert(char *s) {
int u = 0, n = strlen(s);
for(int i = 0; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) {
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u]++;
}
}; //ac自己主动机
int last[maxn], f[maxn];
void print(int i, int j) {
if(j) {
shit += val[j]; //cout << i << endl;
print(i, last[j]);
}
} int getFail() {
queue<int> q;
f[0] = 0;
for(int c = 0; c < SIGMA_SIZE; c++) {
int u = ch[0][c];
if(u) {
f[u] = 0; q.push(u); last[u] = 0;
}
}
while(!q.empty()) {
int r = q.front(); q.pop();
for(int c = 0; c < SIGMA_SIZE; c++) {
int u = ch[r][c];
if(!u) {
ch[r][c] = ch[f[r]][c];
continue;
}
q.push(u);
int v = f[r];
while(v && !ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} void find_T(char* T) {
int len = strlen(T); //cout << len << endl;
int j = 0;
for(int i = 0; i < len; i++) {
int c = idx(T[i]);
j = ch[j][c];
if(val[j]) print(i, j);
else if(last[j]) print(i, last[j]);
}
}
string text[100000+100];
char tmp[10050], tt[105000];
int ans[100000+10000];
int main() {
//freopen("input.txt", "r", stdin);
int T; cin >> T;
while(T--) {
scanf("%d%d", &n, &k);
memset(ans, 0, sizeof(ans[0])*(n+10));
Trie trie;
for(int i = 0; i < n; i++) {
scanf("%s", tmp);
text[i] = tmp;
}
for(int i = 0; i < k; i++) {
scanf("%s", tmp); //cout << tmp << endl;
trie.insert(tmp);
}
getFail();
for(int i = 0; i < n; i++) {
shit = 0;
find_T((char*)text[i].c_str());
ans[i] = shit;
}
for(int i = 0; i < n; i++) printf("%d\n", ans[i]);
}
return 0;
}
HDU 5384 Danganronpa (AC自己主动机模板题)的更多相关文章
- hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数
http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franc ...
- NYOJ 1085 数单词 (AC自己主动机模板题)
数单词 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 为了可以顺利通过英语四六级考试,如今大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中能够通过六 ...
- HDOJ 5384 Danganronpa AC自己主动机
AC自己主动机裸题 Danganronpa Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java ...
- HDU 2222 Keywords Search(AC自己主动机模板题)
题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...
- 【HDU】病毒侵袭(AC自己主动机模板题)
AC自己主动机的模板题.因为输入的字符串中的字符不保证全为小写字母.所以范围应该在130之前,而前31位字符是不可能出如今字符串的(不懂得查下ACSII表即可了).所以仅仅须要开的结点数组大小为130 ...
- AC自己主动机模板
AC自己主动机模板-- /* * AC自己主动机模板 * 用法: * 1.init() : 初始化函数 * 2.insert(str) : 插入字符串函数 * 3.build() : 构建ac自己主动 ...
- Hdu 5384 Danganronpa (AC自动机模板)
题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222 Keywords ...
- hdu2222--Keywords Search+AC自己主动机模板
题目链接:pid=2222">点击进入 KMP对模式串进行处理.然后就能够方便的推断模式串是否在目标串中出现了:这显示适合一个模式串多个目标串的情况.可是假设模式串有多个,这时假设还用 ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
随机推荐
- 删除heroku上的数据库记录
部署本地项目到heroku上.在线上插入数据到数据库,本地代码再次更新到heroku,线上的数据记录还存在单是图片丢失.问题还没有解决: 本地代码和heroku代码怎样同步? heroku使用的pg和 ...
- List of content management systems
https://en.wikipedia.org/wiki/List_of_content_management_systems Microsoft ASP.NET Name Platform Sup ...
- LBP(Local Binary Patterns)局部二进制模式
1. LBP 用于人脸识别 为了预测每个像素属于哪个脸部器官(眼睛.鼻子.嘴.头发),通常的作法是在该像素周围取一个小的区域,提取纹理特征(例如局部二值模式),再基于该特征利用支持向量机等浅层模型分类 ...
- 8.queue
#include <iostream> #include <stack> #include <algorithm> #include <list> #i ...
- TrueOS 不再想要成为“桌面 BSD”了
作者: John Paul Wohlscheid 译者: LCTT geekpi | 2018-07-13 22:53 TrueOS 很快会有一些非常重大的变化.今天,我们将了解桌面 BSD 领域将会 ...
- TypeError: 'dict' object is not callabled
Traceback (most recent call last): File "/root/Desktop/JuniperBackdoor-master/censys.py", ...
- [笔记-统计学习方法]感知机模型(perceptron) 原理与实现
前几天认把感知机这一章读完了,顺带做了点笔记 现在把笔记做第三次的整理 (不得不说博客园的LaTex公式和markdown排版真的不太舒服,该考虑在服务器上建一个博客了) 零.总结 适用于具有线性可分 ...
- Linux服务器性能评估与优化
一.影响务器性能因素 影响企业生产环境Linux服务器性能的因素有很多,一般分为两大类,分别为操作系统层级和应用程序级别.如下为各级别影响性能的具体项及性能评估的标准: (1)操作系统级别 内存: C ...
- Test-我喜欢LInux
测试发帖流程 哈哈 习惯一下先.
- PHP -Casbin: 支持 ACL、RBAC、ABAC 多种模型的 PHP 权限管理框架
PHP-Casbin 是一个用 PHP 语言打造的轻量级开源访问控制框架( https://github.com/php-casbin... ),目前在 GitHub 开源.PHP-Casbin 采用 ...