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. jq或zp监听input的value改变问题

    $(document).on('input propertychange','#citySelectorValue',function () { alert("s"); } 以上J ...

  2. uvm_verision——告诉我你几岁了?

    uvm_version 定义了UVM相关的版本信息,而具体的uvm_revision则是通过在src/macros/uvm_version_defines.svh实现的. uvm_revision_s ...

  3. 破解MySQL和修改mysql的密码

    /etc/init.d/mysql stop mysqld_safe --user=mysql --skip-grant-tables --skip-networking & mysql -u ...

  4. IOS博客

    http://www.cnblogs.com/lovecode/articles/2249548.html从这个人这里了解了一些关于uiview和uilayer的区别 以及对于渲染和动画也有了一些了解 ...

  5. python简单爬虫爬取百度百科python词条网页

    目标分析:目标:百度百科python词条相关词条网页 - 标题和简介 入口页:https://baike.baidu.com/item/Python/407313 URL格式: - 词条页面URL:/ ...

  6. 第三届上海市大学生网络安全大赛wp&学习

    wp 0x00 p200 先分析了程序关键的数据结构 分析程序逻辑,在free堆块的时候没有清空指针,造成悬挂指针,并且程序中给了system('/bin/sh'),可以利用uaf 脚本如下: 1.先 ...

  7. shell脚本,awk合并一列的问题。

    文件 file2内容如下:0 qwert1 asdfghjk2 asdjkl2 zxcvbn3 dfghjkll4 222224 tyuiop4 bnm 让第一列相等的合并成一行,不要第一列,也就是变 ...

  8. UIViewAnimationOptions

    常规动画属性设置(可以同时选择多个进行设置) UIViewAnimationOptionLayoutSubviews:执行UIView动画时,自动更新Subview的Layout约束.. UIView ...

  9. HashMap允许将null用作键 也允许将null作为值

    HashMap不能保证元素的顺序,HashMap能够将键设为null,也可以将值设为null. 与之对应的是Hashtable,(注意大小写:不是HashTable),Hashtable不能将键和值设 ...

  10. ssh 免密码登录 与 密钥公钥原理讲解

    前言 由于最近频繁需要登录几个服务器,每次登录都需要输入密码,故相对麻烦. 由于个人服务器用于实验,故对安全性要求不是很高,故想实现ssh免密登录. 通过阅读ssh 公钥私钥认证操作及原理以及ssh公 ...