Oulipo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6227    Accepted Submission(s):
2513

Problem Description
The French author Georges Perec (1936–1982) once wrote
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.

 
Input
The first line of the input file contains a single
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.

 
Output
For every test case in the input file, the output
should contain a single number, on a single line: the number of occurrences of
the word W in the text T.

 
Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
 
Sample Output
1
3
0
 
Source
 
   ac自动机做法:
   
 
代码:
 #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自动机)的更多相关文章

  1. 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 ...

  2. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  3. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  4. 2017多校第6场 HDU 6096 String AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...

  5. Hdu 5384 Danganronpa (AC自动机模板)

    题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222  Keywords ...

  6. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  7. Keywords Search - HDU 2222(AC自动机模板)

    题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串.   分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...

  8. hdu 6096---String(AC自动机)

    题目链接 Problem Description Bob has a dictionary with N words in it.Now there is a list of words in whi ...

  9. HDU 2296 Ring [AC自动机 DP 打印方案]

    Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...

随机推荐

  1. Python基础、 内置函数

    一.概述 Python中内置了很多函数: 可以通过help().dir()方式查看函数的功能,使用内置函数通常效率更高 abs() abs函数接收一个数字对象,返回它的绝对值,如果接受的对象不是数字抛 ...

  2. 使用ocr的自动备份还原ocr

    1.查看ocr自动备份ocrconfig -showbackup 2.停止所有节点的集群件 3.还原ocr文件ocrconfig -restore <file-name> 4.重启crs, ...

  3. install MCR in silent mode linux server

    ./install -mode silent -agreeToLicense yes -destinationFolder /home/yanzhh/wq/Programs/MCR export LD ...

  4. paper 108:系统学习数字图像处理之图像复原与重建

    首先,必须注意这里所限制的处理条件. 关于图像退化/复原模型 退化的图像是由成像系统的退化加上额外的噪声形成的. 1.只考虑噪声引起的退化 噪声模型,包含于空间不相关和相关两种,除了空间周期噪声,这里 ...

  5. sql join 优化

    项目查询列表,需要新关联一张表,于是就让组下小伙更改了下sql语句,当再次进入列表查询时查询时间一下子就翻倍.那小伙找了半天没找原因. 于是我就打开代码查看,关联的一张表数据非常多,用的left jo ...

  6. curl提交数据时中文乱码

    1.使用curl提交数据时中文乱码解决: <?php $testJSON=array('name'=>'中文字符串','value'=>'test'); foreach ( $tes ...

  7. Excel 统计在某个区间内数值的个数

    =COUNTIF(A1:A50,"<=1.0E-5")-COUNTIF(A1:A50,"<60")

  8. app安装位置声明

    AndroidManifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" ...

  9. C# 中的"yield"使用

    yield是C#为了简化遍历操作实现的语法糖,我们知道如果要要某个类型支持遍历就必须要实现系统接口IEnumerable,这个接口后续实现比较繁琐要写一大堆代码才能支持真正的遍历功能.举例说明 usi ...

  10. EasyUI中在表单提交之前进行验证

    使用EasyUi我们可以在客户端表单提交之前进行验证,过程如下:只需在onSubmit的时候使用return  $("#form1").form('validate')方法即可,E ...