POJ 3461 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算法详细流程讲解
POJ 3461 Oulipo的更多相关文章
- POJ 3461 Oulipo(乌力波)
POJ 3461 Oulipo(乌力波) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] The French autho ...
- 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 ...
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3461 Oulipo 【KMP统计子串数】
传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submission ...
- POJ 3461 Oulipo(模式串在主串中出现的次数)
题目链接:http://poj.org/problem?id=3461 题意:给你两个字符串word和text,求出word在text中出现的次数 思路:kmp算法的简单应用,遍历一遍text字符串即 ...
- POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用
题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...
- poj 3461 Oulipo,裸kmp
传送门 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32373 Accepted: 13093 Desc ...
- 字符串hash - POJ 3461 Oulipo
Oulipo Problem's Link ---------------------------------------------------------------------------- M ...
- HDU 1686 Oulipo , 同 POJ 3461 Oulipo (字符串匹配,KMP)
HDU题目 POJ题目 求目标串s中包含多少个模式串p KMP算法,必须好好利用next数组,, (kmp解析)——可参考 海子的博客 KMP算法 //写法一: #include<string ...
随机推荐
- android拍照选择图片上传服务器自定义控件
做android项目的时候总免不了遇到图片上传功能,虽然就是调用android系统的拍照和相册选择功能,但是总面部了把一大推代码写在activity里,看上去一大推代码头都昏了.不如把这些功能都集成一 ...
- C#输入输出重定向
当 Process 将文本写入其标准流中时,通常将在控制台上显示该文本.通过重定向 StandardOutput 流,可以操作或取消进程的输出.例如,可以筛选文本.用不同方式将其格式化,也可以将输出同 ...
- 【等待事件】序列等待事件总结(enq: SQ - contention、row cache lock、DFS lock handle和enq: SV - contention)
[等待事件]序列等待事件总结(enq: SQ - contention.row cache lock.DFS lock handle和enq: SV - contention) 1 BLOG文档结 ...
- nodejs创建一个HTTP服务器 简单入门级
const http = require('http');//请求http.createServer(function(request, response){ /*createServer该函数 ...
- 深入剖析 Spring 框架的 BeanFactory
说到Spring框架,人们往往大谈特谈一些似乎高逼格的东西,比如依赖注入,控制反转,面向切面等等.但是却忘记了最基本的一点,Spring的本质是一个bean工厂(beanFactory)或者说bean ...
- 如何解决ajax跨域问题(转)
由 于此前很少写前端的代码(哈哈,不合格的程序员啊),最近项目中用到json作为系统间交互的手段,自然就伴随着众多ajax请求,随之而来的就是要解决 ajax的跨域问题.本篇将讲述一个小白从遇到跨域不 ...
- MyCat 学习笔记 第十篇.数据分片 之 ER分片
1 应用场景 这篇来说下mycat中自带的er关系分片,所谓er关系分片即可以理解为有关联关系表之间数据分片.类似于订单主表与订单详情表间的分片存储规则. 本文所说的er分片分为两种: a. 依据主键 ...
- oracle归档日志写满错误解决方法
最近一年,手头上负责的项目要部署到很多个地方,由于项目组里没有人对oracle比较熟悉,只能给自己增加一个DBA的角色了.由于短时间内要部署很多单位,备份策略没有设置好,结果过了一个月,用户报告程序开 ...
- Aptana studio 3前端开发编辑器推荐
直接进入主题,先上图 这就是我Apatana studio 3的默认界面,推荐此工具的原因主要有以下几点: 1.可以集成Emmet,快速编写HTML+CSS,做到效率倍增. 2.Jquery 自动完成 ...
- 07_旅行商问题(TSP问题,货郎担问题,经典NPC难题)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P61 问题9: 问题描述:有n(n<=15)个城市,两两之间均有道路直接相连,给出每两个城市i和j之间的道路长度L[i][j],求 ...