Oulipo

Time Limit: 1000ms
Memory Limit: 65536KB
 
This problem will be judged on PKU. Original ID: 3461
64-bit integer IO format: %lld      Java class name: Main
 

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
 解题:KMP,看不懂,网上找了点代码,研究研究,这份代码写得很俊啊!
 
 #include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int MaxN = ;
char word[MaxN/], txt[MaxN];
int next[MaxN/];
void KMP_next(char b[], int pre[]) {
int n = strlen(b), k;
pre[] = -;
k = -;
for(int i = ; i < n; i++) {
while(k > - && b[k+] != b[i]) k = pre[k];
if(b[k+] == b[i]) k++;
pre[i] = k;
}
} int main() {
int n;
scanf("%d%*",&n);
while(n--) {
gets(word);
gets(txt);
KMP_next(word, next);
int cnt = , len = strlen(word);
for(int i = , j = -; txt[i]; ++i) {
while(j > - && word[j+] != txt[i]) j = next[j];
if(word[j+] == txt[i]) j++;
if(j == len-) {
cnt++;
j = next[j];
}
}
printf("%d\n", cnt);
}
return ;
}

Source

 
貌似这样写。。。。更优化,但是对此题而言,无用
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = ;
int fail[maxn];
void getNext(const char *pStr, int *nextArr) {
int i = , j = -, pLen = strlen(pStr);
nextArr[i] = j;
while (i < pLen) {
if (pStr[++i] != pStr[++j]) {
nextArr[i] = j;
while (j != - && pStr[i] != pStr[j]) j = nextArr[j];
} else nextArr[i] = nextArr[j];
}
}
char word[maxn],text[maxn];
int main(){
int n,ret;
scanf("%d",&n);
while(n--){
scanf("%s %s",word,text);
getNext(word,fail);
for(int i = ret = ,j = ; text[i]; ++i){
while(j != - && word[j] != text[i]) j = fail[j];
if(!word[++j]) ret++;
}
printf("%d\n",ret);
}
return ;
}

整理以后的,可以选择开启所谓的优化

 #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = ;
int fail[maxn];
char word[maxn],text[maxn];
void getFail() {
fail[] = -;
fail[] = ;
for(int i = ,j = -; word[i]; ++i) {
while(j != - && word[i] != word[j]) j = fail[j];
fail[i+] = ++j;
if(word[i+] == word[j]) fail[i+] = fail[j];//使用此句加优化,注释掉不加优化,都是正确的
}
}
int main() {
int n,ret;
scanf("%d",&n);
while(n--) {
scanf("%s%s",word,text);
getFail();
for(int i = ret = , j = ; text[i] ; ++i) {
while(j > - && word[j] != text[i]) j = fail[j];
if(!word[++j]) {ret++;j = fail[j];}
}
printf("%d\n",ret);
}
return ;
}

BNUOJ 3580 Oulipo的更多相关文章

  1. C++之路进阶——poj3461(Oulipo)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35694   Accepted: 14424 Descript ...

  2. poj3461 Oulipo(KMP模板)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17795   Accepted: 7160 Descripti ...

  3. BNUOJ 52325 Increasing or Decreasing 数位dp

    传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...

  4. Match:Oulipo(POJ 3461)

     Oulipo 题目大意:给你一个字符串,要你找到字符串包含指定子串的个数 只要你知道了KMP,这一题简直不要太简单,注意STL的string是会超时的,还是乖乖用char吧 #include < ...

  5. bnuoj 24251 Counting Pair

    一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...

  6. KMP算法 hdu4686 Oulipo

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  7. hdu----1686 Oulipo (ac自动机)

    Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  8. 字符串hash - POJ 3461 Oulipo

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

  9. POJ 3461 Oulipo

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

随机推荐

  1. 基于.net core封装的xml序列化,反序列化操作

    需求: 由于在.net core中去除了Xml序列化XmlSerializer操作类.因此,在于一此数据传输当中出,需要用到对xml格式字符串的处理问题.因此封装了一个xml序列化与反序列化操作的类库 ...

  2. IDEA JavaSE环境配置

    需指定JDK路径: File -> Project Structure -> Project -> Project SDK -> New -> 选择JDK所在的根目录

  3. JS语法学习笔记

    JS语法: JS知识点一览图 JS知识点一览图 在function中写this,在本function中可以显示,写Person则显示undefined. 在function中写Person,在func ...

  4. Java、Node.js、PHP还是.Net? 无论你选谁,我都能教你一招!

    七夕如期而至,不该来的终究还是来了.再傲娇的单身贵族恐怕也难免在今天会感觉一丝丝的空虚.还好你关注了我,因为接下来我准备了三大招教你一个人…..也可以优雅地过七夕. 招式一:移形幻影,无中生有 七夕当 ...

  5. libxml2.dylb 导致<libxml/tree.h> 老是找不到头文件

    添加了libxml2.dylb的framework ,结果还是引用不了<libxml/tree.h>,  老是提示找不到头文件. 这个问题其实比较容易解决,但是XCode的版本问题确实让开 ...

  6. 使用Google Colab训练神经网络(二)

    Colaboratory 是一个 Google 研究项目,旨在帮助传播机器学习培训和研究成果.它是一个 Jupyter 笔记本环境,不需要进行任何设置就可以使用,并且完全在云端运行.Colaborat ...

  7. BestCoder Round#15 1002-Instruction

    http://acm.hdu.edu.cn/showproblem.php?pid=5083 官方题解——> 1002 Instruction 先考虑编码,首先找到operation对应的编码, ...

  8. 漫谈使用Kafka作为MQ中间件

    哪些场景适合使用Kafka线上系统会实时产生数以万计的日志信息,服务器运行状态,用户行为记录,业务消息 等信息,这些信息需要用于多个不同的目的,比如审计.安全.数据挖掘等,因此需要以分类的方式将这些信 ...

  9. 【线段树】bzoj3585: mex

    非常精妙的线段树题 Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. Input 第一行n,m. 第二行为n个数. 从第三 ...

  10. python中enumerate()函数的用法

    描述: enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中.其英文意为:枚举,列举. 函数说明: 语法 ...