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自动机的模板题. ...
随机推荐
- javac编译带包的java文件需要在命令处加参数
不带包:javac aaa.java 带包:javac -d . aaa.java 带包就是 java文件中含有 package com.aaa;
- java面试题:Linux
Q:Linux怎么查端口?端口被占用怎么办? netstat -ntulp | grep 2181//查看2181端口号 netstat -pan | grep 2181 //查看2181端口号 如下 ...
- MySql安装和基本管理&mysql语句
MySql安装和基本管理 本章内容: mysql的安装.启动 mysql破解密码 统一字符编码 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下 ...
- 第四章 栈与队列(d)队列接口与实现
- PAT1131(dfs)
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- 如何在比较1.5 len的次数下,找到整型数组最大最小值
2016-11-11 #include <iostream> #include<stdlib.h> #include<stdio.h> using namespac ...
- Android Studio 增加按钮响应事件
在xml文件里增加android:onClick属性 比如: android:onClick=doanything 然后写响应方法 public void doanything(View v) { . ...
- chorem浏览器无法下载
下载的时候指定字节的长度 context.Response.AddHeader("Content-Length", bytes.Length.ToString()); contex ...
- 【APT】SqlServer游标使用
use [ElephantCredit] go begin transaction tran_bank; print '**脚本开始执行!'; declare @tran_error int , @n ...
- 引入flash
调用代码 <div class="media"> <object class="main_video_box" classid="c ...