HDOJ Oulipo 1686【KMP】
Oulipo
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7200 Accepted Submission(s): 2867
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.
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.
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
1
3
0
KMP模板题。注意k的赋值,题目要求是包括几个 不是能切割几个。如asdasd asdasdasd 这样的情况下结果应该是2 所以当k==plen后再计算,k的赋值不能从0再開始,而是从pre[k]開始。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 1000000+10 using namespace std; int pre[maxn];
char T[maxn];
char P[10010]; void getpre(int plen)
{
pre[0]=pre[1]=0;
for(int i=1;i<plen;i++){
int k=pre[i];
while(k&&P[i]!=P[k])k=pre[k];
pre[i+1]=P[i]==P[k]? k+1:0;
}
} int search(int slen,int plen)
{
int num=0;
getpre(plen);
int k=0;
for(int i=0;i<slen;i++){
while(k&&P[k]!=T[i])k=pre[k];
if(P[k]==T[i])k++;
if(k==plen){
++num;
k=pre[k]; //从0開始是挪动了plen。题目是找出包括几个,不是能切割几个。所以从pre[k]開始。
}
}
return num;
} int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(pre,0,sizeof(pre));
scanf("%s%s",P,T);
int slen=strlen(T);
int plen=strlen(P);
int res=search(slen,plen);
printf("%d\n",res);
}
return 0;
}
HDOJ Oulipo 1686【KMP】的更多相关文章
- HDOJ 2203 亲和串 【KMP】
HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- 【KMP】Censoring
[KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...
- 【KMP】OKR-Periods of Words
[KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...
- 【KMP】Radio Transmission
问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...
- 【kmp】似乎在梦中见过的样子
参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...
- 【POJ3461】【KMP】Oulipo
Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
随机推荐
- So注入工具TsoInject开发文档
So注入工具TsoInject开发文档 导语: 作为一个软件安全从业者而言,我们需要对某个App的关键函数就行Hook, 对于android而言,Smali层我们使用Xposed Hook框架,So层 ...
- javaScript 笔记(5) --- jQuery(上)
这节整理整理 iquery.js 相关的内容... 目录 --- jQuery 语法 --- 文档就绪事件 --- jQuery 选择器 --- jQuery 事件 --- jQuery 效果 jQu ...
- 转 C++拷贝构造函数详解
C++拷贝构造函数详解 一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: int a = 100; int b = a; 而类对象与普通对象不同,类对象内部结构一 ...
- LeetCode OJ--Same Tree
https://oj.leetcode.com/problems/same-tree/ 判断两个二叉树,是否相等. 深搜 /** * Definition for binary tree * stru ...
- andorid人员文件上传服务器的搭建(tomcat中)
1.将.war文件复制到tomcat服务器webapps下,启动服务器即可 2.访问工程路径http://localhost:8080/FileUpload/index.jsp即可测试上传 3.测试成 ...
- AC日记——郁闷的出纳员 codevs 1286
郁闷的出纳员 思路: 设工资下限为ko,然后ko--(因为要小于工资下限): 设cur为记录工资增长,降低: 设第i个人的工资为pos: 对应的四种操作: 插入:cur-pos-ko: 增长:cur- ...
- 网站防火墙探测工具Wafw00f
网站防火墙探测工具Wafw00f 现在网站为了加强自身安全,通常都会安装各类防火墙.这些防火墙往往会拦截各种扫描请求,使得测试人员无法正确判断网站相关信息.Kali Linux提供了一款网站防火墙探 ...
- POJ 1067 取石子游戏 [博弈]
题意:威佐夫博弈. 思路:看了很多证明都没看懂.最后决定就记住结论好了. 对于所有的奇异局面(必败局),有通项公式 Pi = (a, b), (a = i * [(sqrt(5) + 1) / 2], ...
- luogu P3116 [USACO15JAN]会议时间Meeting Time
题目描述 Bessie and her sister Elsie want to travel from the barn to their favorite field, such that the ...
- delphi 按位运算 not and or xor shl shr
delphi 按位运算 not and or xor shl shr unit Unit1; interface uses Windows, Messages, SysUtils, Var ...