C++之路进阶——poj3461(Oulipo)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 35694 | Accepted: 14424 |
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
题解:
kmp经典例题,因为题目说可压缩,所以末尾+1的失败指针指向1,1的失败指针指向0,就ok了。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 1000000 using namespace std; int next[maxn],n;
char W[maxn],T[maxn]; void getnext()
{
int j=,k=-;next[]=-;
while (!j||W[j]!=)
{
if (k==-||W[j]==W[k])
{
j++,k++;
if (W[j]!=W[k]) next[j]=k;else next[j]=next[k];
}else k=next[k];
}
} int kmp()
{
int i=,j=,num=;
memset(next,,sizeof(next));
int T_len=strlen(T),W_len=strlen(W);
getnext();
while (i<T_len)
{
if (j==-||T[i]==W[j]) i++,j++;
else j=next[j];
if (j==W_len)
{
num++;
j=next[j];
}
}
return num;
} int main()
{
scanf("%d",&n);
for (int i=;i<=n;i++)
{
scanf("%s%s",W,T);
printf("%d\n",kmp());
}
return ;
}
C++之路进阶——poj3461(Oulipo)的更多相关文章
- C++之路进阶codevs1269(匈牙利游戏)
1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description ...
- poj3461 Oulipo(KMP模板)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17795 Accepted: 7160 Descripti ...
- C++之路进阶——优先队列优化最短路径算法(dijkstra)
一般的dijkstra算法利用贪心的思想,每次找出最短边,然后优化到其他点的的距离,我们还采用贪心思路,但在寻找最短边进行优化,之前是双重for循环,现在我们用优先队列来实现. 代码解释: //样例程 ...
- poj3461 Oulipo
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
- KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period
首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...
- C++之路进阶——HDU1880(魔咒词典)
---恢复内容开始--- New~ 欢迎参加2016多校联合训练的同学们~ 魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 3 ...
- C++之路进阶——P2022
P2022 有趣的数 让我们来考虑1到N的正整数集合.让我们把集合中的元素按照字典序排列,例如当N=11时,其顺序应该为:1,10,11,2,3,4,5,6,7,8,9. 定义K在N个数中的位置为Q( ...
- C++之路进阶codevs1242(布局)
1242 布局 2005年USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold <:section class="hbox" ...
- C++之路进阶——codevs3333(高级打字机)
3333 高级打字机 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 早苗入手了最新的高级打字机.最新款自然有着与 ...
随机推荐
- 使用python解析Json字符串-获取Json字符串关键字
import json data = { "statusCode": 0, "data": { ", "height": &quo ...
- maven ClassNotFoundException: org.springframework.web.context.ContextLoaderL
信息: Starting Servlet Engine: Apache Tomcat/6.0.32 2012-3-31 9:39:40 org.apache.catalina.core.Standar ...
- Javascript实现页面加载完成后自动刷新一遍清除缓存文件
我们有些时候在加载页面时,会出现缓存文件对当前文件的表现效果有干扰,如有些缓存的样式文件会是页面效果发生改变,这时我们希望页面在加载时能自动刷新一遍清楚缓存文件. 但是由于跳转页面肯定会用到BOM部分 ...
- Linux:使用nohup让进程在后台可靠运行
学习之余我最大的乐趣是找一部不错的电影慢慢品味,这也是我缓解压力的最好方式之一,由于我常去的字幕组网站需要签到才可以下载字幕,像这种娱乐网站谁有时间天天记得去签到呢,but作为一个准程序猿应该有更好的 ...
- poj1131-Octal Fractions(进制转换)
一,题意: 求一个八进制小数的十进制.二,思路: 暴力数组模拟计算,注意千万不带小数做除法运算 1,对于八进制小数,转换成十进制,书写形式分析: 2,对其除法过程进行模拟: 3,输出. 三,步骤: 1 ...
- asp.net identity 3.0.0 在MVC下的基本使用(一)
注册时信箱转为用户名. 本人习惯使用用户名做注册用户,因为不管是什么终端起码都能少输入几个字符,可以提高用户体验. 这里需要更改控制器,模型和视图 1.打开Controllers目录下的Account ...
- 自动执行任务管理---TaskManage
这篇主要配合数据使用 先说数据库 名字都很标准---------------------------------- ------------------------------------------ ...
- 《UML大战需求分析》阅读笔记06
对<UML>撒站需求分析的阅读现在已到达尾声,虽然读这本书是应老师的要求,但是在后期的阅读中也逐渐喜欢上这本书,在阅读的过程中不断领会需求分析与UML之间的联系,让在软件需求分析阶段有了更 ...
- Runtime详解
http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/
- cookie的session_id解释
HTTP协议(http://www.w3.org/Protocols/)是“一次性单向”协议. 服务端不能主动连接客户端,只能被动等待并答复客户端请求.客户端连接服务端,发出一个HTTP Reques ...