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. SpringMVC之 HandlerAdapter和handlerMapping

    HandlerAdapter字面上的意思就是处理适配器,它的作用用一句话概括就是调用具体的方法对用户发来的请求来进行处理.当handlerMapping获取到执行请求的controller时,Disp ...

  2. cookie和session基础以及在Django中应用

    看了会视频,终于搞懂了~ 1.cookie cookie:保存状态 cookie的工作原理是:由服务器产生内容,浏览器收到请求后保存在本地:当浏览器再次访问时,浏览器会自动带上cookie,这样服务器 ...

  3. jQuery和AJAX基础

    jQuery和AJAX基础 jQuery 基础: 1.jQuery 选择器: 元素选择器:$("p"): #id 选择器:$("#test"): .class ...

  4. 电脑公司最新稳定win7系统下载

    系统来自系统妈:http://www.xitongma.com 系统概述 电脑公司ghost win7 x86(32位)万能装机版集成的软件符合电脑公司及电脑城装机绝大多数人要求及喜好,既大众,又时尚 ...

  5. codevs 1008 选数

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n ...

  6. 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows

    题目描述 Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths tha ...

  7. Linux Device Driver 学习(1)

    Linux Device Driver 学习(1) 一.搭建虚拟机开发环境 1.选择虚拟机VirtualBox,官网下载.deb包安装: VirtualBox Linux 5.1.6 下载fedora ...

  8. HDOJ1195 双向BFS //单向也可以过 没想清

    #include<cstdio> #include<map> #include<vector> #include<stack> #include< ...

  9. Android(java)学习笔记146:网页源码查看器(Handler消息机制)

    1.项目框架图: 2.首先是布局文件activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com ...

  10. CPP-基础:关于内存分配

    1:c中的malloc和c++中的new有什么区别 (1)new.delete 是操作符,可以重载,只能在C++中使用.(2)malloc.free是函数,可以覆盖,C.C++中都可以使用.(3)ne ...