hdu----1686 Oulipo (ac自动机)
Oulipo
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6227 Accepted Submission(s):
2513
a book, La disparition, without the letter 'e'. He was a member of the Oulipo
group. A quote from the book:
Tout avait Pair normal, mais tout
s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain,
l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait
au roman : stir son tapis, assaillant à tout instant son imagination,
l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un
non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la
raison : tout avait l’air normal mais…
Perec would probably have scored
high (or rather, low) in the following contest. People are asked to write a
perhaps even meaningful text on some subject with as few occurrences of a given
“word” as possible. Our task is to provide the jury with a program that counts
these occurrences, in order to obtain a ranking of the competitors. These
competitors often write very long texts with nonsense meaning; a sequence of
500,000 consecutive 'T's is not unusual. And they never use spaces.
So we
want to quickly find out how often a word, i.e., a given string, occurs in a
text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finite
strings over that alphabet, a word W and a text T, count the number of
occurrences of W in T. All the consecutive characters of W must exactly match
consecutive characters of T. Occurrences may overlap.
number: the number of test cases to follow. Each test case has the following
format:
One line with the word W, a string over {'A', 'B', 'C', …, 'Z'},
with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
One line
with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤
1,000,000.
should contain a single number, on a single line: the number of occurrences of
the word W in the text T.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<queue> using namespace std;
const int maxn=; struct node{ node *child[maxn];
node * fail ; //失败指针
int tail ; };
void init(node * tmp){
for(int i=;i<maxn;i++)
tmp->child[i]=NULL;
tmp->fail=NULL;
tmp->tail=;
} //建立一颗字典树 void Buildtree(const char ss [] ,node * root){
node *cur;
for(int i=; ss[i] ; i++){
int no =ss[i]-'A';
if(root->child[no]==NULL){
cur = new node ;
init(cur);
root->child[no]=cur;
}
root = root->child[no];
}
root->tail++;
} //构造失败指针
void BuildFail(node * root){ queue<node *> tmp;
tmp.push(root);
node *cur,*po;
while(!tmp.empty()){
cur = tmp.front();
tmp.pop(); //出队列
for(int i= ; i<maxn ; i++){
if( cur->child[i] != NULL ){
if(cur==root){
cur->child[i]->fail=root; //指向树根
}else{
po = cur ;
while(po->fail){
if(po->fail->child[i]){
cur->child[i]->fail=po->fail->child[i];
break;
}
po = po->fail;
}
if(po->fail==NULL)
cur->child[i]->fail=root;
}
tmp.push(cur->child[i]);
}
}
}
} //查询
int Query(char ss [] , node *root){ node *cur , *tmp;
cur =root;
int pos=-,res=; for(int i=; ss[i] ;i++){ pos= ss[i]-'A';
while(cur->child[pos]==NULL&&cur!=root)
cur = cur->fail;
cur = cur->child[pos];
if(cur==NULL) cur=root;
tmp = cur;
while(tmp!=root&&tmp->tail>){
res+=tmp->tail;
tmp = tmp->fail;
}
} return res; }
char sa[],sb[];
int main(){
int te;
node *root;
scanf("%d",&te);
while(te--){
root = new node ;
init(root);
scanf("%s %s",sa,sb);
Buildtree(sa,root);
BuildFail(root);
int ans =Query(sb,root);
printf("%d\n",ans);
}
return ;
}
hdu----1686 Oulipo (ac自动机)的更多相关文章
- HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)
HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...
- HDU - 1686 Oulipo KMP匹配运用
id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- 2017多校第6场 HDU 6096 String AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...
- Hdu 5384 Danganronpa (AC自动机模板)
题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222 Keywords ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- Keywords Search - HDU 2222(AC自动机模板)
题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串. 分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...
- hdu 6096---String(AC自动机)
题目链接 Problem Description Bob has a dictionary with N words in it.Now there is a list of words in whi ...
- HDU 2296 Ring [AC自动机 DP 打印方案]
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...
随机推荐
- ios每日一发--Leanclude数据云存储以及登录 注册账户
利用LeanCloud来实现注册账号,存储账号以及,登录时查询账号是否正确.集成方式很简单可以看这里的官方文档.地址是这里: https://leancloud.cn/docs/ 在这里创建应用,以及 ...
- RDIFramework.NET ━ 9.16 案例模块━ Web部分
RDIFramework.NET ━ .NET快速信息化系统开发框架 9.15 案例模块 -Web部分 9.16.1.产品管理模块 产品管理模块提供了基本的增.删.改.查.导出.分页等的实现,用户可 ...
- Maven聚合与继承的实例讲解(一)
概述 在javaweb高速发展的今天,我们软件设计人员往往会用很多种方式对软件划分模块,目的就是为了能有清晰的设计和低耦合性的,高重用性的软件.Maven有很好的依赖管理系统(Dependency M ...
- matlab编译器和程序发布
如何把编写好的matlab程序转换成c/c++语言,如何编译m文件为可执行程序,如何在没有matlab配置环境的机器上运行你编译出来的可执行程序? 一:matlab的编译器设置 执行命令:mbuild ...
- Leetcode: Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- Android View坐标Left, Right, Top, Bottom
Android View坐标Left, Right, Top, Bottom 1.分析说明Left, Right, Top, Bottom View中对于该字段说明如下,相对父布局的的位置 相对父布局 ...
- flexigrid扩展(添加全选,格式化表单)
1.关于flexigrid的属性这里不多说,百度一大把. 这里主要说一个参数process process参数:编写对某一列进行自定义处理的函数 function 方法名(tddiv,pid){}// ...
- Dynamics AX 2012 R2 安装额外的AOS
众所周知,AX系统分为三层:Client,Application Server,Database Server. 我们添加额外的Application Server主要是出于以下两个原因: 使用多台服 ...
- GAT2.0使用文档(组合接口测试)
3.2接口用例场景组件 在此之前,大家应该都已经开发完成了一个最简单的接口测试用例,但是之前的接口用例的期望结果是固定值,不能动态的去做对比,有很大局限性.下面开始介绍怎样通过场景组件来动态对测试结果 ...
- Linux之保留yum安装软件后的RPM包
yum安装软件很方便,但是下载下来的rpm包在安装后默认会被删除掉: 如果希望保留yum安装的软件包该如何做呢? 设置方法: 将/etc/yum.conf里对应的keepcache参数改为1即可,然后 ...