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自动机的模板题. ...
随机推荐
- SolarWinds网络管理手册列表
前段时间使用过SolarWinds管理思科的交换机,在使用的过程中自己做了一个简单的使用手册,SolarWinds是一款非常强悍的网管软件,手册中没有完全涉及SolarWinds的所有功能,Solar ...
- hdu 5154 拓扑排序
例题:hdu 5154 链接 http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思是第一行先给出n和m表示有n件事,m个关系,接下来输入m行,每行有 ...
- 解决IDEA查看源码时提示:Library source does not match the bytecode for class的问题分析
解决方法:
- ZOJ2018/4月月赛G题Traffic Light(广搜)
题意:首先T组数据,每组数据包括:第一行:一个n,m,然后下面有一个n行m列的01矩阵. 最后一行输入四个数字,分别是起点的横纵坐标,终点的横纵坐标.询问从起点到终点,最少要几步,如果到不了输出-1 ...
- label 和input/textarea居中对齐
设置label和input/textarea的vertical-align: middle;即可实现垂直方向居中对齐.有时候可能会有偏差,设置input的margin-top使看上去居中对齐
- 理解HTTP之keep-alive(转)
理解HTTP之keep-alive 在前面一篇文章中讲了TCP的keepalive,这篇文章再讲讲HTTP层面keep-alive.两种keepalive在拼写上面就是不一样的,只是发音一样,于是乎大 ...
- GridView和DataFormatString 日期格式 精确小数点后位数
如果DataFormatString无效,请添加属性 HtmlEncode = "false" --------------------------------------- Da ...
- Java面试基础知识(1)
1.final, finally, finalize的区别 final:修饰符(关键字)如果一个类被声明为final,没有子类也不能被继承.因此一个类不能既被声明为 abstract的,又被声明为fi ...
- 运行./build.sh出现错误:bash: ./build.sh: Permission denied
原因:文件为只读,或者用户没有权限. 修改方法: 运行命令: chmod 777 build.sh
- EntityFramework的linq扩展where
代码 using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; ...