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
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自动机做法:
代码:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #include<iostream>
5 #include<queue>
6
7 using namespace std;
8 const int maxn=26;
9
10 struct node{
11
12 node *child[maxn];
13 node * fail ; //失败指针
14 int tail ;
15
16 };
17 void init(node * tmp){
18 for(int i=0;i<maxn;i++)
19 tmp->child[i]=NULL;
20 tmp->fail=NULL;
21 tmp->tail=0;
22 }
23
24
25 //建立一颗字典树
26
27 void Buildtree(const char ss [] ,node * root){
28 node *cur;
29 for(int i=0; ss[i] ; i++){
30 int no =ss[i]-'A';
31 if(root->child[no]==NULL){
32 cur = new node ;
33 init(cur);
34 root->child[no]=cur;
35 }
36 root = root->child[no];
37 }
38 root->tail++;
39 }
40
41 //构造失败指针
42 void BuildFail(node * root){
43
44 queue<node *> tmp;
45 tmp.push(root);
46 node *cur,*po;
47 while(!tmp.empty()){
48 cur = tmp.front();
49 tmp.pop(); //出队列
50 for(int i=0 ; i<maxn ; i++){
51 if( cur->child[i] != NULL ){
52 if(cur==root){
53 cur->child[i]->fail=root; //指向树根
54 }else{
55 po = cur ;
56 while(po->fail){
57 if(po->fail->child[i]){
58 cur->child[i]->fail=po->fail->child[i];
59 break;
60 }
61 po = po->fail;
62 }
63 if(po->fail==NULL)
64 cur->child[i]->fail=root;
65 }
66 tmp.push(cur->child[i]);
67 }
68 }
69 }
70 }
71
72 //查询
73 int Query(char ss [] , node *root){
74
75 node *cur , *tmp;
76 cur =root;
77 int pos=-1,res=0;
78
79 for(int i=0; ss[i] ;i++){
80
81 pos= ss[i]-'A';
82 while(cur->child[pos]==NULL&&cur!=root)
83 cur = cur->fail;
84 cur = cur->child[pos];
85 if(cur==NULL) cur=root;
86 tmp = cur;
87 while(tmp!=root&&tmp->tail>0){
88 res+=tmp->tail;
89 tmp = tmp->fail;
90 }
91 }
92
93 return res;
94
95 }
96 char sa[10010],sb[1000010];
97 int main(){
98 int te;
99 node *root;
100 scanf("%d",&te);
101 while(te--){
102 root = new node ;
103 init(root);
104 scanf("%s %s",sa,sb);
105 Buildtree(sa,root);
106 BuildFail(root);
107 int ans =Query(sb,root);
108 printf("%dn",ans);
109 }
110 return 0;
111 }
- 如何调用finecms指定栏目的描述关键词
- finecms栏目文章页seo设置
- Logistic回归实战篇之预测病马死亡率(一)
- DedeCMS后台500错误一种原因是不支持PHP5.3、5.4及以上版本
- finecms指定从第几篇文章开始调用5条记录,并调用文章所在栏目
- finecms如何调用多个栏目的子栏目
- 从傅立叶变换到Gabor滤波器
- 三个小时学会wordpress模板制作
- The each() function is deprecated报错的解决方法
- 书接上文:薛定谔的猫是如何诞生的?
- docker源码分析(3)---镜像(1)
- k8s源码分析-----kubelet(8)pod管理
- 大会 | DiracNets:无需跳层连接的ResNet
- golang时间戳格式化与解析
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 详解Linux下crontab的使用与注意事项
- Linux内核设备驱动之Linux内核基础笔记整理
- Ubuntu18.04 Server版安装及使用(图文)
- Ubuntu18.04安装vsftpd的实现代码
- ubuntu系统theano和keras的安装方法
- Linux安装Jenkins步骤及各种问题解决(页面访问初始化密码)
- 解决Ubuntu19 安装Theano问题
- centos7 esxi6.7模板实际应用详解
- Centos8搭建本地Web服务器的实现步骤
- 总结Linux 6种日志查看方法
- Ubuntu18.04一次性升级Python所有库的方法步骤
- linux下php安装xml扩展的详细步骤
- 查看linux文件的命令详解
- 解决Linux+Apache服务器URL区分大小写问题
- Centos8(最小化安装)全新安装Python3.8+pip的方法教程