【POJ3461】Olipo
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
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; char A[+],B[+];
int T[+];
int n,ans; void calc_T()//找到失配函数
{
T[]=-;
int j,lenA=strlen(A);
for (int i=;i<lenA;i++)
{
j=T[i];
while (j!=- && A[j]!=A[i]) j=T[j];
T[i+]=++j;
}
} void kmp(int lenA)
{
calc_T();
int j=,k=,lenB=strlen(B);
while (k<lenB && j<lenA)
{
if (k==- || A[j]==B[k]) j++,k++;
else k=T[k];
if (k==lenB) ans++,k=T[k];
}
//return ans;
} int main()
{
while (~scanf("%d",&n))
{
for (int i=;i<=n;i++)
{
ans=;
scanf("%s%s",B,A);
kmp(strlen(A));
printf("%d\n",ans);
}
}
}
Code
【POJ3461】Olipo的更多相关文章
- 【POJ3461】【KMP】Oulipo
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
- 【poj3461】 Oulipo
http://poj.org/problem?id=3461 (题目链接) 题意 求一个字符串在另一个字符串中出现的次数. Solution KMP裸题,太久没写过了,都忘记怎么求next数组了..水 ...
- 【POJ3461】Oulipo
题面 The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- 【原】谈谈对Objective-C中代理模式的误解
[原]谈谈对Objective-C中代理模式的误解 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 这篇文章主要是对代理模式和委托模式进行了对比,个人认为Objective ...
- 【原】FMDB源码阅读(三)
[原]FMDB源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 FMDB比较优秀的地方就在于对多线程的处理.所以这一篇主要是研究FMDB的多线程处理的实现.而 ...
- 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新
[原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...
- 【调侃】IOC前世今生
前些天,参与了公司内部小组的一次技术交流,主要是针对<IOC与AOP>,本着学而时习之的态度及积极分享的精神,我就结合一个小故事来初浅地剖析一下我眼中的“IOC前世今生”,以方便初学者能更 ...
- Python高手之路【三】python基础之函数
基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...
随机推荐
- [LeetCode] Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- C# Qrcode生成二维码支持中文,带图片,带文字 2015-01-22 15:11 616人阅读 评论(1) 收藏
1.下载Qrcode库源码,下载地址:http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library 2.打开源码时,部分类库 ...
- .NET NLog 详解 (三) - LayoutRender
这期将NLog Git版本指向2005-06-09,NLog v0.9 released.这个时候的代码结构升级为这样: 和上期的版本相比,最明显的莫过于原先的Appender全套更名为Target. ...
- thinkphp计划任务使用cronRun
thinkphp计划任务使用cronRun .先不管是是否是独立分组,必须在你项目名下的Conf文件夹内创建2个文件一个是tages.php 一个是 crons.php. 注意这两个文件名为think ...
- WPF 实现带标题的TextBox
这篇博客将分享在WPF中如何创建一个带Title的TextBox.首先请看一下最终的效果, 实现思路:使用TextBlock+TextBox来实现,TextBlock用来显示Title. 实现代码, ...
- winows下使用ssh服务远程登录vbox中的虚拟机
1.到http://www.putty.org/下载并安装SSH客户端 2.查看是否安装ssh服务 在ubuntu终端命令界面键入: #ssh localhost 如果出现下面提示则表示还没有安装: ...
- WPF线程(Step2)——BackgroundWorker
在WPF中第二个常用的线程处理方式就是BackgroundWorker. 以下是BackgroundWorker一个简单的例子. public partial class MainWindow : W ...
- 数字信号处理实验(六)——FIR滤波器的设计
一.四种线性相位FIR滤波器的振幅响应 1.自编函数 [Hr,w,a,L]=-n) [Hr,w,a,L]=-n) [Hr,w,a,L]=-n) [Hr,w,a,L]=-n) 2.一个demo clea ...
- merge布局
当LayoutInflater遇到这个标签时,它会跳过它,并将<merge />内的元素添加到<merge />的父元素里.迷惑了吗?让我们用<merge />来替 ...
- 使用while代替for循环的几个习题
1:兔子问题 2:100以内质数的和 3:单位给发了一张150元购物卡,拿着到超市买三类洗化用品.洗发水15元,香皂2元,牙刷5元.求刚好花完150元,有多少种买法,没种买法都是各买几样? 总结:wh ...