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 ...
随机推荐
- 使用MyEclipse编写Java程序
MyEclipse是非常实用的一款Java程序开发工具,主要用于Java.Java EE以及移动应用的开发.MyEclipse的功能非常强大,支持也十分广泛,尤其是对各种开源产品的支持相当不错. My ...
- php中命名空间和use
php中命名空间和use 总结 php中的namespace就有点像java中package包的概念 php中的use的概念就是用别人的命名空间中的类 php中的include enquire是引入文 ...
- spark 从RDD createDataFrame 的坑
Scala: import org.apache.spark.ml.linalg.Vectors val data = Seq( (7, Vectors.dense(0.0, 0.0, 18.0, 1 ...
- 32.AngularJS 表达式
转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 表达式写在双大括号内:{{ expression }}. AngularJS 表达式把数据 ...
- rest_framework 认证功能
from django.views import View from rest_framework.views import APIView from rest_framework.authentic ...
- [NOIP2015模拟10.22] 最大子矩阵 解题报告(单调栈)
Description 我们将矩阵A中位于第i行第j列的元素记作A[i,j].一个矩阵A是酷的仅当它满足下面的条件: A[1,1]+A[r,s]<=A[1,s]+A[r,1](r,s ...
- Optional arguments
We have seen built-in functions that take a variable number of arguments. For example range can take ...
- String build-in function - len
len is a build-in function that returns the numbers of characters in a string: Since we started coun ...
- 安卓开发--AsyncTask
package com.cnn.asynctask; import android.app.Activity; import android.content.Intent; import androi ...
- Kali linux 2016.2(Rolling)中metasploit的端口扫描
目前常见的端口扫描技术一般有如下几类: TCP Connect.TCP SYN.TCP ACK.TCP FIN. Metasploit中的端口扫描器 Metasploit的辅助模块中提供了几款实用的 ...