AC自动机 HDOJ 5384 Danganronpa
/*
题意:多个文本串,多个模式串在每个文本串出现的次数
AC自动机:这就是一道模板题,杭电有道类似的题目
*/
/************************************************
* Author :Running_Time
* Created Time :2015-8-14 14:14:32
* File Name :AC.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e5 + ;
const int MAXNODE = 6e5 + ;
const int INF = 0x3f3f3f3f;
const int SIGMA_SIZE = ;
const int MOD = 1e9 + ;
ll res;
struct AC {
int ch[MAXNODE][SIGMA_SIZE], f[MAXNODE], sz;
ll val[MAXNODE];
void init(void) {
memset (ch[], , sizeof (ch[]));
sz = ; val[] = ;
}
int idx(char c) {
return c - 'a';
}
void insert(string s) {
int u = ; int len = s.size ();
for (int i=; i<len; ++i) {
int c = idx (s[i]);
if (!ch[u][c]) {
memset (ch[sz], , sizeof (ch[sz]));
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u]++;
}
void build(void) {
queue<int> Q; f[] = ;
for (int i=; i<SIGMA_SIZE; ++i) {
int u = ch[][i];
if (u) {
f[u] = ; Q.push (u);
}
}
while (!Q.empty ()) {
int r = Q.front (); Q.pop ();
for (int i=; i<SIGMA_SIZE; ++i) {
int u = ch[r][i];
if (!u) {
ch[r][i] = ch[f[r]][i]; continue;
}
Q.push (u);
f[u] = ch[f[r]][i];
val[u] += val[f[u]];
}
}
}
ll query(string s) {
int len = s.size ();
ll ret = ; int j = ;
for (int i=; i<len; ++i) {
int c = idx (s[i]);
j = ch[j][c];
ret += val[j];
}
return ret;
}
}ac;
string t[MAXN], p; int main(void) { //HDOJ 5384 Danganronpa
int T; scanf ("%d", &T);
while (T--) {
int n, m; scanf ("%d%d", &n, &m);
ac.init ();
for (int i=; i<=n; ++i) cin >> t[i];
for (int i=; i<=m; ++i) {
cin >> p; ac.insert (p);
}
ac.build ();
for (int i=; i<=n; ++i) {
printf ("%I64d\n", ac.query (t[i]));
}
} return ;
}
AC自动机 HDOJ 5384 Danganronpa的更多相关文章
- HDOJ 5384 Danganronpa AC自己主动机
AC自己主动机裸题 Danganronpa Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java ...
- AC自动机 HDOJ 2222 Keywords Search
题目链接 题意:每个文本串的出现次数 分析:入门题,注意重复的关键字算不同的关键字,还有之前加过的清零. 新模板,加上last跑快一倍 #include <bits/stdc++.h> ...
- Hdu 5384 Danganronpa (AC自动机模板)
题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222 Keywords ...
- HDU 5384——Danganronpa——————【AC自动机】
Danganronpa Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- 【HDOJ 5384】Danganronpa
[HDOJ 5384]Danganronpa AC自己主动机. .. 当时感觉用字典树 标神也往自己主动机想来着..手太生加上时间紧迫也没敲--回来一看题解什么AB同一时候建自己主动机...顿时愣了 ...
- HDU 5384 AC自动机
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:给n个母串,给m个匹配串,求每个母串依次和匹配串匹配,能得到的数目和. 分析:之前并不知道AC ...
- HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...
- AC自动机+DP HDOJ 2457 DNA repair(DNA修复)
题目链接 题意: 给n串有疾病的DNA序列,现有一串DNA序列,问最少修改几个DNA,能使新的DNA序列不含有疾病的DNA序列. 思路: 构建AC自动机,设定end结点,dp[i][j]表示长度i的前 ...
- hdoj 3065 病毒侵袭持续中(AC自动机)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 思路分析:问题需要模式匹配多个模式串,需要注意的是模式串会包含和重叠,需要对AC自动机的匹配过 ...
随机推荐
- vue Iframe
1.Iframe.vue <!-- Iframe --> <template> <div> <!-- 标题栏 --> <mt-header tit ...
- [Android]自己定义带删除输入框
在项目开发中,带删除button输入框也是人们经常常使用到的,该文章便介绍一下怎样创建一个带删除输入框.当中,须要解决的问题例如以下: a)创建自己定义editText类 b)在自己定义editTex ...
- Eclipse项目遇到问题汇总
1:gc overhead limit exceeded 原因:这是由于项目中eclipse内存分配不足导致 修改:修改eclipse.ini文件 修改如下: ...
- [读书笔记]《没人会告诉你的PPT真相》
这本书分了三部分.第一部分偏重于基础技能,其中分为三部分,打印.放映.保存.第二部分是进阶,分为模板下载.模板修改.增加自定义页面等.第三部分是打造商业范的PPT,分为商业范的特征,具体技能体现(重复 ...
- Codeforces 755 F. PolandBall and Gifts 多重背包+贪心
F. PolandBall and Gifts It's Christmas time! PolandBall and his friends will be giving themselves ...
- qrcode.react和jquery.qrcode生成二维码
qrcode.react 1.安装 npm install qrcode.react 2.用法(这里用的ant design) import React from 'react'; import QR ...
- USING REFLECTION
In this section, you take a closer look at the System.Type class, which enables you to access inform ...
- Lightoj 1011 - Marriage Ceremonies
You work in a company which organizes marriages. Marriages are not that easy to be made, so, the job ...
- 一步一步学Silverlight 2系列(4):鼠标事件处理
一步一步学Silverlight 2系列(4):鼠标事件处理 概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言V ...
- 深入探析c# Socket
最近浏览了几篇有关Socket发送消息的文章,发现大家对Socket Send方法理解有所偏差,现将自己在开发过程中对Socket的领悟写出来,以供大家参考. (一)架构 基于TCP协议的Socket ...