POJ 3461 Oulipo(乌力波)

Time Limit: 1000MS   Memory Limit: 65536K

【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.

法国作家乔治·佩雷克(1936–1982)曾经不用字母'e'写了《消失》这本书。他是乌力波组织的一员。书中写道:

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…(法语……卒)

佩雷克将在随后的竞赛中取得或高或低的成绩。人们被要求写出关于某个主题的意味深长的文章,并尽可能少使用给定的“关键字”。我们的任务就是为评委会提供关键字统计程序,以此来决定选手的名次。这些人的文章通常又臭又长,还不用空格;比如500,000个连续的'T'。

因此我们想快速查询给定字符串在文章中的出现的次数。进一步说:给定字母表{'A', 'B', 'C', …, 'Z'}和由其中字母组成的两个有限串,一个关键字W和一段文章T,统计W在T中出现的次数。W中的连续字符必须完全匹配T中的连续字符。匹配的字符可能会发生重叠。

【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.

输入文件的第一行是一个数字表示随后测试用例的数量。每个测试用例格式如下:

  • 一行是关键字W,由{'A', 'B', 'C', …, 'Z'}中的元素组成的字符串,并且1 ≤ |W| ≤ 10,000(此处|W|表示字符串W的长度)。
  • 一行文章T由{'A', 'B', 'C', …, 'Z'}中的元素组成的字符串,并且|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.

对于每个测试用例输出一个数字一行,表示关键字W在文章T中出现的次数。

【Sample Input - 输入样例】

【Sample Output - 输出样例】

3

BAPC

BAPC

AZA

AZAZAZA

VERDI

AVERDXIVYERDIAN

1

3

0

【题解】

KMP的入门题,关于KMP主要思想是在比对的时候利用已经比对过的元素减少额外的比对次数。大概就是用next记录前缀后缀最长公共元素的长度来实现下标的跳转,减少比较次数。(网上的解释更加详细,此处不再赘述)

需要注意的应该就是处理好字符串的边界了。

【代码 C++】

 #include<cstdio>
#include<cstring>
#define mx 1000005
char w[mx], t[mx];
int next[mx], wED, tED;
void rdy(){
int i = , j;
next[] = j = -;
while (i <= wED){
if (j == - || w[i] == w[j]) next[++i] = ++j;
else j = next[j];
}
w[wED] = '#';
}
int count(){
int opt = , iw = , it = ;
while (it < tED){
while (w[iw] == t[it] || iw == -) ++iw, ++it;
if (iw == wED) ++opt;
iw = next[iw];
}
return opt;
}
int main(){
int n;
scanf("%d", &n); getchar();
while (n--){
gets(w); gets(t);
wED = strlen(w); tED = strlen(t);
rdy();
printf("%d\n", count());
}
return ;
}

感谢飘过的小牛巨的代码提供的代码简略思路

POJ 3461 Oulipo(乌力波)的更多相关文章

  1. 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 ...

  2. POJ 3461 Oulipo

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  3. POJ 3461 Oulipo[附KMP算法详细流程讲解]

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  4. POJ 3461 Oulipo 【KMP统计子串数】

    传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...

  5. POJ 3461 Oulipo(模式串在主串中出现的次数)

    题目链接:http://poj.org/problem?id=3461 题意:给你两个字符串word和text,求出word在text中出现的次数 思路:kmp算法的简单应用,遍历一遍text字符串即 ...

  6. POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用

    题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...

  7. poj 3461 Oulipo,裸kmp

    传送门 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32373   Accepted: 13093 Desc ...

  8. 字符串hash - POJ 3461 Oulipo

    Oulipo Problem's Link ---------------------------------------------------------------------------- M ...

  9. HDU 1686 Oulipo , 同 POJ 3461 Oulipo (字符串匹配,KMP)

    HDU题目 POJ题目 求目标串s中包含多少个模式串p KMP算法,必须好好利用next数组,, (kmp解析)——可参考 海子的博客  KMP算法 //写法一: #include<string ...

随机推荐

  1. UIViewController卸载过程(ios6.0之前)

    1.当应用程序收到内存不足的警告之后,程序中所有存在的UIViewController都会收到didReceiveMemoryWarning调用消息,目的是将当前不显示的View释放掉,缓解内存压力. ...

  2. 由于 add 运算符中“Chinese_PRC_CI_AS”和“Chinese_PRC_CS_AS_WS”之间的排序规则冲突

    有一个字段的排序规则是 Chinese_PRC_CS_AS_WS,字符串连接的时候报错. 处理方案 cast(columnName as varbinary) 即可

  3. 编译busybox-1.24.1 制作文件系统

    arm-linux-gcc  3.4.5 busybox-1.24.1.tar.bz21, 修改 Makefile找到以下2处修改为ARCH ?= armCROSS_COMPILE ?= arm-li ...

  4. Hibernate解决n+1问题

    观点:对于n+1问题的理解. 一般而言说n+1意思是,无论在一对多还是多对一当查询出n条数据之后,每条数据会关联的查询1次他的关联对象,这就叫做n+1. 但是我的理解是,本来所有信息可以一次性查询出来 ...

  5. How To Create a SSL Certificate on Apache for CentOS 6

    About Self-Signed Certificates 自签证书.一个SSL证书,是加密网站的信息,并创建更安全的链接的一种方式.附加地,证书可以给网站浏览者显示VPS的的身份证明信息.如果一个 ...

  6. hibernate核心接口,和扩展接口。回顾笔记,以前没记,现在补上,纯手工敲的。

    hibernate核心接口: 所有的hibernate应用都会访问hibernate的5个核心接口 1,Configuration接口 Configuration用于配置并且根启动Hibernate. ...

  7. iOS完整App资源收集

    前言 iOS开发学习者都希望得到实战训练,但是很多资料都是只有一小部分代码,并不能形成完成的App,笔者在此处收集了很多开源的完整的App,都有源代码哦! 本篇文章持续更新中,请持续关注.本篇所收集的 ...

  8. Centos7下用命令下载jdk7及jboss-eap-6

    计划在南非的一台云主机上搭建一个web环境,首先需要在云主机上搭建我们指定版本的JDK和JBOSS. 在云上搭特定版本的环境,软件包传输是一件十分艰巨的任务.我先后尝试了:公司电信专线.公司联通专线. ...

  9. ectouch第十一讲 之 ECTouch 菜单里如何添加文章链接

    1.首先在 ectouch 后台添加好文章分类和文章内容(具体添加方法很简单,这里就不再赘述),然后在菜单管理里添加导航如下(链接地址的获取方法参考步骤 2):2.文章分类列表 域名/mobile/i ...

  10. JavaScript DOM 编程艺术(第2版)读书笔记(6)

    案例研究:图片库改进版 我们在学校里学过一种理论,叫做结构化程序设计.其中有这样一条原则:函数应该只有一个入口和一个出口.从理论上讲,我很赞同这项原则:但在实际工作中,过分拘泥于这项原则往往会使代码变 ...