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 ...
随机推荐
- ERROR 1010 (HY000): Error dropping database (can't rmdir '.\qpweb', errno: 41) 删库失败问题的解决
Win8 下,MySQL5.5,root 用户登录 MySQL 5.5 Command Line Client,删除 qpweb 数据,执行命令 drop database qpweb;报错信息:ER ...
- 【转】数据库系统异常排查之DMV
数据库系统异常是DBA经常要面临的情景,一名有一定从业经验的DBA,都会有自己一套故障排查的方法和步骤,此文为为大家介绍一下通过系统 性能视图(SQLServer05以上版本)来排查系统异常的基本方法 ...
- JAVA经典算法40题(1-20)
[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律 ...
- JQuery拖拽排序
1,引用JqueryUI $(function(){ $(".m_title").bind('mouseover',function(){ $(this).css("cu ...
- Datatable的查找和排序(Datatable.Select)
Datatable 是一种常用的数据结构.数据类型有点类似于数据库中的表结构.在没有使用优秀的orm框架前,大部分的数据库的数据都是先变为Datatable 然后再通过代码转换变成 object. ...
- python windows安装
一.下载并安装 下载地址http://www.python.org/download/ 安装 二.配置环境变量 配置python环境变量以便后面安装插件.D:\Program Files\Python ...
- SQL2005中的事务与锁定(五)- 转载
------------------------------------------------------------------------ -- Author : HappyFlyStone - ...
- 关于Navigation的BarButtonItem的
(ios6.1)有两个controller在navigation stack里,A和B.A是B的previous view controller,现在top-level controller是B.要自 ...
- 连续最短路算法(Successive Shortest Path)(最小费用最大流)
#include <cstdio> #include <cstring> #include <queue> #include <vector> #inc ...
- ADT中通过Android SDK Manager去安装x86的image时无法下载
参考:http://www.crifan.com/adt_android_sdk_manager_can_not_download_package_x86_image/