POJ - 3461 Oulipo(KMP模板题)

来源:互联网 发布:阿里云smtp服务器地址 编辑:程序博客网 时间:2024/05/20 02:25

B - Oulipo

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

Submit Status Practice POJ 3461

Description

The Frenchauthor Georges Perec (1936–1982) once wrote a book, La disparition, without theletter 'e'. He was amember of the Oulipo group. A quote from the book:

Tout avait Pairnormal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puissurgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulaitl’association qui l’unissait au roman : stir son tapis, assaillant à toutinstant 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 commandanttout, où s’abolissait la raison : tout avait l’air normal mais…

Perec wouldprobably have scored high (or rather, low) in the following contest. People areasked to write a perhaps even meaningful text on some subject with as fewoccurrences of a given “word” as possible. Our task is to provide the jury witha program that counts these occurrences, in order to obtain a ranking of thecompetitors. These competitors often write very long texts with nonsensemeaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.

So we want toquickly find out how often a word, i.e., a given string, occurs in a text. Moreformally: given the alphabet {'A''B''C', …, 'Z'} and two finitestrings over that alphabet, a word W and a text T,count the number of occurrences of W in T. All theconsecutive characters of W must exactly match consecutive characters of T.Occurrences may overlap.

Input

The first lineof 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 testcase in the input file, the output should contain a single number, on a singleline: the number of occurrences of the word W in thetext T.

Sample Input

3

BAPC

BAPC

AZA

AZAZAZA

VERDI

AVERDXIVYERDIAN

Sample Output

1

3

0

 KMP模板题。

#include<iostream>#include<string>#include<cstring>#include<cstdio>#include <stdlib.h>          //题目大意:   给出主串和模式串,求模式串在主串中出现的次数(部分可以重合)using namespace std;         //暴力匹配自然会 TLE                             //用KMP,利用上next[ ]数组的含义,每次不全部退出,而是退出部分j=next[ j ]int nextl[1000000];char str[1000000];char word[100000];int len;int Slen;void get_nextlen(int n){int i = 0, j = -1;nextl[0] = -1;while (i <= n){if (j == -1 || word[i] == word[j]){j++; i++;nextl[i] = j;}else j = nextl[j];}return;}int Kmp(int u){int i = u - 1, j = -1, m = 0;     //字符串从0开始存储的while (i <= Slen){if (j == -1 || str[i] == word[j]){i++; j++;}elsej = nextl[j];if (j >= len)         //**注意这里要j--,i--,因为上面匹配成功了i和j的值加多了1{m++;j--; i--;j = nextl [j];      //返回}}return m;}int main(){int casen;cin >> casen;while (casen--){memset(nextl, 0, sizeof(nextl));cin >> word;cin >> str;len = strlen(word);Slen = strlen(str);get_nextlen(len);int ans = Kmp(0);          //Kmp从第一个字符开始匹配cout << ans << endl;}}


1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 公司外派异地工作医保怎么办 老鼠死在墙里面怎么办 父亲再婚婚后对我不好怎么办 皮衣搽了护理油后不亮了怎么办 新买的衣服皱了怎么办 新买的裙子很皱怎么办 新买的风衣很皱怎么办 新买的裙子皱了怎么办 货拉拉准点率低怎么办 定机票名字打错了怎么办 做坏事被发现了怎么办 在阳台做被发现怎么办 有秘密被发现了怎么办 微店没有收到货怎么办 cf与主机连接不稳定怎么办 穿越火线与主机连接不稳定怎么办 word被锁定无法编辑怎么办 平衡车系统乱了怎么办 监控老是滴滴的响怎么办 磁盘已满 文件未保存怎么办 cocos只有代码没有项目怎么办 电脑系统管理员密码忘记了怎么办 魔兽小队不显示职业颜色怎么办 魔兽多余的橙装怎么办? f117-f6不读硬盘怎么办 中飞院飞行学生停飞了怎么办 军人对你敬礼时怎么办 小孩抱着就睡放下就醒怎么办 着火了怎么办 我的世界 生存战争2感冒了怎么办 生存战争2吐了怎么办 我的世界hqm重置怎么办 不小心打了110怎么办 我的世界皮肤有黑影怎么办 我的世界字体变大了怎么办 生锈的铁钉扎了怎么办 每天晚上窗纱上老有蝙蝠倒挂怎么办 我的世界没有痒怎么办 七日杀被ban了怎么办 吕框箱子上保护摸撕不掉怎么办 我的世界开光影卡怎么办