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. Mssql链接mysql数据库

    最近在做mysql数据库实时同步到mssql数据库的方案,花了一周时间,测试通过了,在实际机器上测试出现了若干问题.第一个问题就是在mssql上链接mysql的问题. 第一步,安装 Mysql ODB ...

  2. 推导大O阶方法

    用大写O()来体现算法时间复杂度的记法,我们称之为大O阶记法. O(1)叫做常数阶:O(n)叫做线性阶:O(n^2)叫做平方阶.  1.用常数1取代运行时间中的所有加法常数. 2.在修改后的运行次数函 ...

  3. 【读书笔记】iOS-开发技巧-UILabel内容模糊的原因

    在非Retina的iPad mini的屏幕上,一个UILabel的frame的origin值如果有小数位数(例如,0.5),就会造成显示模糊.所以最好用整数值的origin坐标. 参考资料: < ...

  4. 【原】iOSCoreAnimation动画系列教程(一):CABasicAnimation【包会】

    本文的最新版本已经发布在简书[编程小翁]上,强烈建议到上查看简书,[点击这里跳转]. 在iOS中,图形可分为以下几个层次: 越上层,封装程度越高,动画实现越简洁越简单,但是自由度越低:反之亦然.本文着 ...

  5. H5文件操作API

    引言 在之前我们操作本地文件都是使用flash.silverlight或者第三方的activeX插件等技术,由于使用了这些技术后就很难进行跨平台.或者跨浏览器.跨设备等情况下实现统一的表现,从另外一个 ...

  6. 数据仓库建模与ETL实践技巧

    数据分析系统的总体架构分为四个部分 —— 源系统.数据仓库.多维数据库.客户端(图一:pic1.bmp) 其中,数据仓库(DW)起到了数据大集中的作用.通过数据抽取,把数据从源系统源源不断地抽取出来, ...

  7. 用java程序输出自己的姓名

    代码部分: public class Hello { public static void main(String[] args) { System.out.println("$$$$$$$ ...

  8. MFC 网络编程 -- 总结

    原文链接:http://www.cnblogs.com/lidabo/archive/2012/07/19/2598734.html 1.基于 TCP 的 socket 编程 /* 服务器端程序流程: ...

  9. 读书笔记——Windows环境下32位汇编语言程序设计(6)使用浮点指令进行64位除法

    罗云彬 典藏版Page192,mark下. 这段代码看不懂,手册上根本没有fdivr不带操作数的指令. .data dqTickCounter1 dq ? dqTickCounter2 dq ? dq ...

  10. nyoj 1029/hdu 3714 Error Curves 三分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3714 懂了三分思想和F(x)函数的单调性质,这题也就是水题了 #include "stdio ...