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自动机的模板题. ...
随机推荐
- 二叉树的镜像(python)
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- 链表中倒数第k个结点(python)
题目描述 输入一个链表,输出该链表中倒数第k个结点 # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.v ...
- Openstack 集群,及常用服务的 高可用 haproxy配置
一.介绍 配置文件位置(yum 安装):/etc/haproxy/haproxy.cfg 全局配置 #------------------------------------------------- ...
- c++ - Linking problems due to symbols with abi::cxx11?
看错误内容: /data/projects/LipReadingSDKGPU/lib/cwlibs/libLipReading.so: undefined reference to `tensorfl ...
- PAT1066(AVL树)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- 音频播放 音乐 MediaPlayer
MediaPlayer对象的生命周期如下: Idle 状态:当使用new()方法创建一个MediaPlayer对象或者调用了其reset()方法时,该MediaPlayer对象处于idle状态.这两种 ...
- 100-days: Four
Title: Weekend 'catch-up sheep' is a lie catch-up n.补做:赶做 play catch-up 通过追赶,达到同样的水平或程度 catch-up sle ...
- Q in Q
简介 Q in Q技术(也称Stacked VLAN 或Double VLAN).标准出自IEEE 802.1ad,将用户私网VLAN Tag封装在公网VLAN Tag中,使报文带着两层VLAN Ta ...
- Rabbitmq基本使用 SpringBoot整合Rabbit SpringCloud Stream+Rabbit
https://blog.csdn.net/m0_37867405/article/details/80793601 四.docker中使用rabbitmq 1. 搭建和启动 使用地址:rabbitm ...
- SparseArray
使用SparseArray更加节省内存空间的使用,SparseArray也是以key和value对数据进行保存的.使用的时候只需要指定value的类型即可.并且key不需要封装成对象类型. Has ...