E - Oulipo

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

hash_AC代码:

/*
Problem: 3461 User: bbsh
Memory: 5296K Time: 110MS
Language: G++ Result: Accepted
*/
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1e6+;
typedef int i64;
i64 S,chk,p,P,hash_key[N];
char s1[N],s2[N];
int cas,l1,l2,ans;
inline i64 fpow(i64 a,i64 p){
i64 res=;
for(;p;p>>=,a=a*a) if(p&) res=res*a;
return res;
}
inline void get_s2_key(){
p=fpow(P,l2);S=;
for(int i=;i<=l2;i++) S=S*P+s2[i]-'A';
}
inline void get_s1_key(){
hash_key[]=;
for(int i=;i<=l1;i++) hash_key[i]=hash_key[i-]*P+s1[i]-'A';
}
inline i64 query(int x,int y){
return hash_key[y]-hash_key[x-]*p;
}
int main(){
P=;//prime_num
for(scanf("%d",&cas);cas--;){
scanf("%s%s",s2+,s1+);ans=;
l1=strlen(s1+);l2=strlen(s2+);
get_s2_key();
get_s1_key();
for(int i=l2;i<=l1;i++){
chk=query(i-l2+,i);
if(chk==S) ans++;
}
printf("%d\n",ans);
}
return ;
}

KMP_AC代码:

/*
Problem: 3461 User: bbsh
Memory: 1424K Time: 110MS
Language: G++ Result: Accepted
Source Code
*/
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1e6+;
char s1[N],s2[N];
int cas,l1,l2,fail[N];
void get_next(){
int p=;fail[]=;
for(int i=;i<=l2;i++){
while(p>&&s2[i]!=s2[p+]) p=fail[p];
if(s2[i]==s2[p+]) p++;
fail[i]=p;
}
}
void kmp(){
int p=,ans=;
for(int i=;i<=l1;i++){
while(p>&&s1[i]!=s2[p+]) p=fail[p];
if(s1[i]==s2[p+]) p++;
if(p==l2) ans++,p=fail[p];
}
printf("%d\n",ans);
}
int main(){
for(scanf("%d",&cas);cas--;){
scanf("%s%s",s2+,s1+);
l1=strlen(s1+);l2=strlen(s2+);
get_next();
kmp();
}
return ;
}

附KMP算法详细流程讲解

  移步shenben's documents system

POJ 3461 Oulipo的更多相关文章

  1. POJ 3461 Oulipo(乌力波)

    POJ 3461 Oulipo(乌力波) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] The French autho ...

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

  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. 【github】github 使用教程初级版

    github 是一个基于 git 的代码托管平台,付费用户可以建私人仓库,免费用户只能使用公共仓库.对于一般人来说公共仓库就已经足够了,而且也没多少代码来管理.下面简单介绍如何使用 github,供初 ...

  2. Atitit.工作流系统的本质是dsl 图形化的dsl  4gl

    Atitit.工作流系统的本质是dsl 图形化的dsl  4gl 1. 工作流系统的本质是dsl 图形化的dsl  4gl1 2. 为什么每个项目系统都需要工作流1 3. 工作流dsl与java .n ...

  3. 安卓开发-问题集-Description Resource Path Location TypeUnparsed aapt error(s)! Check the console for output.

    今天在安卓项目中 res-drawable-hdpi 替换图片的时候出现这个问题 问题现象为项目显示一个叉号,但是在项目内容的任何文件都不显示叉号, 搞了半天没发现是什么问题,然后就去项目的文件夹下 ...

  4. RunLoop机制理解

    一.浅识RunLoop RunLoop在开发中我们一直在用,但是没有注意他.要想理解RunLoop,首先我们需要先了解一下程序运行机制. 程序运行机制:我们都知道OC是运行时语言,也就是说对象的类型是 ...

  5. Cent OS 6.4安装mysql

    Cent OS6.4 RPM安装mysql 一.卸载掉原有mysql 因为目前主流Linux系统版本基本上都集成了mysql数据库在里面 如下命令来查看我们的操作系统上是否已经安装了mysql数据库 ...

  6. 使用CocoaPods管理第三方开源类库

    iOS开发中经常会用到许多第三方开源类库,比如AFNetworking.FMDB.JSONKit等等,使用CocoaPods这个工具就能很方便得对工程中用到的类库进行管理,包括自动下载配置以及更新. ...

  7. Cocos2d入门--2--三角函数的应用

    其实,三角函数的知识点是初中的数学基础.但是在编程里合理的利用的话,也会很好玩的,可以制作出很多有趣的动画特效. 首先要注意的是 角度 和 弧度 的转换. 360度 = 2×PI弧度 180度 =   ...

  8. (第五章)java面向对象之this的作用总结

    this关键字总是指向调用该方法的对象. this可以代表任何对象,当this出现在某个方法体中时,它所代表的对象是不确定的,但它的类型是确定的,它所代表的对象只能是当前类的(在那个类中就是那个类), ...

  9. linux下修改系统时间

    一.查看时间: [root@localhost ~]# date2016年 11月 19日 星期六 12:46:37 CST 二.修改时间,修改系统时间 [root@localhost ~]# dat ...

  10. 每日Scrum--No.7

    Yesterday:学习和设计路线的编程 Today:编写代码 Problem:.在设计查询参观路线的时候,整个逻辑特别的混乱,设想了各种树,图以及网的遍历问题,但经过多次与同学的交流以及网上的查询资 ...