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 ...
随机推荐
- KingBlog记录
本片文章其他人可能看不懂,只是我做记录用,不要见怪 KingBlogCMS总结 开发工具: VS2012+SOLServer2008+IIS 开发语言: ASP.NET.Html.js.JQ 第三方: ...
- linux command screen
喜欢这句话 “我们永远相信,分享是一种美德 | We Believe, Great People Share Knowledge...” 但是这种美德不是为什么都要问的人准备的 ——————————— ...
- For,Function,Lazy
package com.dtgroup.study import scala.io.Source object ForFunctionLazy { def main(args: Array[Strin ...
- 文件上传(java web)
文件上传: 对表单的要求: * method="post" * enctype="multipart/form-data" * 表单中需要添加文件表单项:< ...
- Hosts简单说明
Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联"数据库",当用户在浏览器中输入一个需要登录的网址时,系统 ...
- android stutio 快捷键
[F] [F] F2 在错误代码之间切换 F3 往前定位(Shift + F3:往后定位 )有问题 F4\Ctrl+鼠标点击\Ctrl+B 转到定义,查看类继承关系 F5 但不调试进入函数内部. F6 ...
- hash命令
什么是hash ? 在网上找了好久都没找到简截有力的说明.hash 我把它当成是集合,一个hash 就是一个集合,里面字段对应一个元素,元素不重复,字段都不一样. 简单hash 命令 1.hset 哈 ...
- 【转】Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If
转载地址:http://fanshuyao.iteye.com/blog/1695482 在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server ...
- 浙江理工2015.12校赛-B 七龙珠
七龙珠 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 781 Solved: 329 Description 话说孙壕请吃了青岛大虾后,一下子变穷了,就 ...
- NSDate获取当前时区的时间
[NSDate date]获取的是GMT时间,要想获得某个时区的时间,以下代码可以解决这个问题 NSDate *date = [NSDate date]; NSTimeZone *zone = [NS ...