POJ 3461:Oulipo
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 28155 | Accepted: 11251 |
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 textT,
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
KMP模板题,没有比这个更模板的了。。。
唯一要提醒自己的是最后模式串和原串走的时候j是一直往前走的,不回头!怎么就记不住,每次都是搞了两层循环,然后TLE,你这么搞之前搞的那个next数组有什么用,动脑袋想一想啊光速小子。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
using namespace std; char moshi[10005];
char pipei[1000005]; int result;
int next_moshi[10005]; void get_next()
{
int i,j=-1;
int len=strlen(moshi);
next_moshi[0]=-1; for(i=0;i<len;)
{
if(j==-1||moshi[i]==moshi[j])
{
i++;
j++; if(moshi[i]==moshi[j])
{
next_moshi[i]=next_moshi[j];
}
else
{
next_moshi[i]=j;
}
}
else
{
j=next_moshi[j];
}
}
} void get_result()
{
int i=0,j=0; int len_pipei=strlen(pipei);
int len_moshi=strlen(moshi); while(j<len_pipei)
{
if(moshi[i]==pipei[j])
{
i++;
j++;
}
else
{
if(next_moshi[i]==-1)
{
i=0;
j++;
}
else
{
i=next_moshi[i];
}
}
if(i>=len_moshi)
{
result++;
i=next_moshi[len_moshi];
}
}
} int main()
{
int Test;
scanf("%d",&Test); while(Test--)
{
result=0; scanf("%s%s",moshi,pipei); get_next();
get_result(); cout<<result<<endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3461:Oulipo的更多相关文章
- 【poj 3461】Oulipo(字符串--KMP)
题意:求子串在文本串中出现了多少次. 解法:使用KMP的next[ ]和tend[ ]数组计数. #include<cstdio> #include<cstdlib> #inc ...
- 【POJ 3461】 Oulipo
[题目链接] 点击打开链接 [算法] KMP [代码] #include <algorithm> #include <bitset> #include <cctype&g ...
- POJ 3461 Oulipo
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3461 Oulipo(乌力波)
POJ 3461 Oulipo(乌力波) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] The French autho ...
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 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 ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- POJ 3461 Oulipo 【KMP统计子串数】
传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submission ...
随机推荐
- Unnatural
1. 纪录片:非自然选择 1.1 CRISPR-Cas9的出现 1.2 故事1:先天性基因缺陷而失明的小孩 1.3 故事2:基因变异的蚊子 1.4 基因技术应用的现状 1.5 担忧 2. CRISPR ...
- SQL常用关键字
常用SQL语句 SAP实际上提供了两种访问数据库的方式:Open SQL与Native SQL ---语句 功能 数据操作 insert 向表添加新数据行 delete 从表中删除数据行 upda ...
- DICOM的Worklist服务
看 DICOM 标准有一段时间了,前面几篇也介绍了一下 DIMSE-C 消息服务,具体参看Dicom 学习笔记-Dicom 消息服务(DIMSE-C/DIMSE-N),本文就介绍一下 DICOM 标准 ...
- nodejs(12)Express 中间件middleware
中间件 客户端的请求到达服务器时,他的生命周期是:request -- 服务器端处理 -- 响应 在服务器端处理过程中,业务逻辑复杂时,为了便于开发维护,需要把处理的事情分成几步,这里每一步就是一个中 ...
- Nginx反向代理实现负载均衡配置图解
Nginx反向代理实现负载均衡配置图解 [导读] 负载均衡配置是超大型机器需要考虑的一些问题,同时也是数据安全的一种做法,下面我来介绍在nginx中反向代理 负载均衡配置图解,大家可参考本文章来操作. ...
- centos 安装 memcached
centos 安装 memcached 1. 安装依赖: libeventyum install libevent-devel 2. 获取最新版本wget http://memcached.org/l ...
- Python Sklearn.metrics 简介及应用示例
Python Sklearn.metrics 简介及应用示例 利用Python进行各种机器学习算法的实现时,经常会用到sklearn(scikit-learn)这个模块/库. 无论利用机器学习算法进行 ...
- 二十二 动态代理&解决网站的字符集编码问题
设计模式: 软件开发过程中,遇到相似问题,将问题的解决方式抽取模型(套路) 单例,工厂,装饰者,适配器,动态代理 谷歌汽车场景: 谷歌汽车场景Car Interface Icar{ start r ...
- 关于目标检测的anchor问题
关于目标检测其实我一直也在想下面的两个论断: Receptive Field Is Natural Anchor Receptive Field Is All You Need 只是一直没有实验.但是 ...
- C#遍历DataSet
] foreach (DataRow dr in dt.Rows) ///遍历所有的行 foreach (DataColumn dc in dt.Columns) //遍历所有的列 Console.W ...