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 早苗入手了最新的高级打字机.最新款自然有着与 ...
随机推荐
- VB下对HTML元素的操作
<!DOCTYPE html> <html> <head> <title>test</title> </head> <bo ...
- bzoj2141排队(辣鸡但是好写的方法)
题意很明确,也非常经典: 一个支持查询 区间中比k大的数的个数 并且支持单点修改的序列 ——因为题意可以转化为:查询这两个数中比后者大的个数.比后者小的个数.比前者大的个数.比前者小的个数(根据这4个 ...
- Android -- 使用图库文件并可以裁剪文件(ImageView)
1. 本例子首先教大家如何打开图库 ,然后在使用裁剪功能,来裁剪图库里面的图片并进行显示工作
- 关于 error: Operation is not valid due to the current state of the object。
今天碰到一个特别的异常. Operation is not valid due to the current state of the object. at System.Web.HttpValueC ...
- About_类与对象02
FCKeditor文本编辑程序(共享软件)为用户提供在线的文档编辑服务,其具有与微软office软件一样的功能,与之不同的是FCKeditor不需要用户安装任何形式的客户端,FCKeditor程序非常 ...
- ZK textbox Constraint验证
test.zul: <?page title="" contentType="text/html;charset=UTF-8"?> <zk x ...
- Android课程---关于GridView网格视图的学习
activity_ui6.xml <?xml version="1.0" encoding="utf-8"?> <GridView xmlns ...
- address_add
<include file="Header:header-address_add" /> <include file="Header:header-pu ...
- js 小知识
在iframe 页面获取父级页面的 html var obj = window.parent.document.getElementById('modaliframe'); 解决Jquery 的在一个 ...
- JAVA继承与覆写
实例:数组操作 首先是开发一个整型数组父类,要求从外部控制数组长度,并实现保存数据以及输出.然后子类中实现排序和反转. 基础父类代码如下: class Array { private int data ...