HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个?
其实就是AC自动机模板题啊( ╯□╰ )
正着query一次再反着query一次就好了
/* gyt
Live up to every day */ #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>`
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = ;
const ll maxm = 1e7;
const ll mod = 1e9 + ;
const int INF = 0x3f3f3f;
const ll inf = 1e15 + ;
const db eps = 1e-;
const int kind=;
struct node{
node *fail;
node *next[kind];
int coun;
void nodee() {
fail=NULL;
coun=;
for (int i=; i<kind; i++)
next[i]=NULL;
}
}*root;
char str[maxn];
char tmp[maxn];
int ans=; void updata() {
node *p=root;
int len=strlen(str);
for (int i=; i<len; i++) {
int pos=str[i]-'A';
if (p->next[pos]==NULL) {
p->next[pos]=new node;
p->next[pos]->nodee();
p=p->next[pos];
}
else p=p->next[pos];
}
p->coun++;
}
void getfail() {
node *p=root, *son, *tmp;
queue<struct node*>que;
que.push(p);
while(!que.empty()) {
tmp=que.front();
que.pop();
for (int i=; i<; i++) {
son=tmp->next[i];
if (son!=NULL) {
if (tmp==root) {
son->fail=root;
}
else {
p=tmp->fail;
while(p) {
if (p->next[i]) {
son->fail=p->next[i];
break;
}
p=p->fail;
}
if (!p) son->fail=root;
}
que.push(son);
}
}
}
}
void query() {
int len=strlen(str);
node *p, *tmp;
p=root;
int cnt=;
for (int i=; i<len; i++) {
int pos=str[i]-'A';
while(!p->next[pos]&& p!=root) p=p->fail;
p=p->next[pos];
if (!p) p=root;
tmp=p;
while(tmp!=root) {
if (tmp->coun>=) {
cnt+=tmp->coun;
tmp->coun=-;
}
else break;
tmp=tmp->fail;
}
}
ans+=cnt;
}
void solve() {
ans=;
root=new node;
root->nodee();
root->fail=NULL;
int n; scanf("%d", &n);
getchar();
for (int i=; i<n; i++) {
gets(str); updata();
}
getfail();
gets(str);
int len=strlen(str);
int nn=; char c;
int now=; int cnt=;
while() {
if (now>=len) break;
if (str[now]=='[') {
now++;
while(str[now]>=''&&str[now]<='') {
nn = nn*+(str[now]-'');
now++;
}
c=str[now];
for (int i=; i<nn; i++)
tmp[cnt++]=c;
nn=;
now++;
now++;
}
else {
// if (str[now]==']') {
// now++; continue;
// }
tmp[cnt++]=str[now]; now++;
}
// cout<<str[now]<<endl;
}
tmp[cnt]='\0';
for (int i=; i<=cnt; i++) str[i]=tmp[i];
// cout<<str<<endl;
query();
reverse(str, str+strlen(str));
// cout<<str<<endl;
query();
cout<<ans<<endl;
}
int main() {
int t = ;
// freopen("in.txt", "r", stdin);
scanf("%d", &t);
while(t--)
solve();
return ;
}
HDU3695(AC自动机模板题)的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDu-2896 病毒侵袭,AC自动机模板题!
病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...
- [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]
AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- HDU-2222 Keywords Search(AC自动机--模板题)
题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...
- HDU 2222 Keywords Search(AC自动机模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...
随机推荐
- 转化为分组背包 zoj 3769
题目链接:https://vjudge.net/problem/ZOJ-3769 题意:现在你要去打怪,你有13种装备,每件装备会有伤害和防御两种属性,一般来说,每种装备只可以装备一件,但是特别的,戒 ...
- 51nod 1459 迷宫游戏 dijkstra模板
链接:迷宫游戏 问题 - 51Nod http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459 1459 迷宫游戏 基准 ...
- MYSQL 插入数据乱码
1.最近在写电商项目 遇见过向数据库中加入数据乱码问题 最开始以为是,数据库的问题但是一看 没问题啊 于是又看了项目的默认编码,也没问题啊 那么问题来了,在哪出现了问题呢 于是 博主 在 tomact ...
- TOJ 5225: 玩转二叉树
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=5225 时间限制(普通/Java): ...
- jdbc java远程连接mysql数据库服务器
首先,需要注意以下几点: 1.手机需要获得可以访问网络的权限: 2.导入的jdbc驱动的版本需要与mysql服务器的版本相近: 3.mysql默认的访客是只允许本机(localhost),不允许其他主 ...
- springMVC数据交互
控制器 作为控制器,大体的作用是作为V端的数据接收并且交给M层去处理,然后负责管理V的跳转.SpringMVC的作用不外乎就是如此,主要分为:接收表单或者请求的值,定义过滤器,跳转页面:其实就是ser ...
- am start的总结,-d参数的总结,以及python中传递内容包含中文及特殊字符&的解决方案
一.am start的内容的整理 主要包含以下内容:am start的常规操作及参数的含义,-d 参数的含义,以及如何在APK中设置参数获取 使用命令如下:adb shell am start -n ...
- TZOJ 3030 Courses(二分图匹配)
描述 Consider a group of N students and P courses. Each student visits zero, one or more than one cour ...
- 【Linux 进程】exec族函数详解
exec族的组成: 在Linux中,并不存在一个exec()的函数形式,exec指的是一组函数,一共有6个,分别是: #include <unistd.h> extern char **e ...
- Django1.0和2.0中的rest_framework的序列化组件之超链接字段的处理
大家看到这个标题是不是有点懵逼,其实我就是想要一个这样的效果 比如我get一条书籍的数据,在一对多的字段中我们显示一个url,看起来是不是很绚! 下面我们就来实现这么一个东西 首先我们一对多字段中的一 ...