Oulipo---KMP
Problem DescriptionThe 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. InputThe 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). OutputFor 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 Input3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN Sample Output1 3 0 |
题目大意:
求在父串中有几个目标串,注意:在父串中上一个与目标串相同的子串可以和下一个与目标串相等的子串共用字符。
AC代码:
#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=10005;
const int maxn1=1000005;
char S[maxn1],T[maxn];
int l1,l2;
int next1[maxn];
void getnext()
{int i=0,j=-1;next1[0]=-1;while(i<l2){if(j==-1||T[i]==T[j]){++i;++j;if(T[i]!=T[j])next1[i]=j;elsenext1[i]=next1[j];}elsej=next1[j];}
}
int kmp()
{int i=0,j=0,t1=0;while(i<=l1&&j<=l2){if(j==-1||S[i]==T[j]){++i;++j;}elsej=next1[j];if(j==l2){ //注意这里是为什么t1++;j=next1[j];}}return t1;
}
int main()
{int t;scanf("%d",&t);while(t--){cin>>T>>S;l1=strlen(S);l2=strlen(T);getnext();cout<<kmp()<<endl;}return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686
Oulipo---KMP相关推荐
- Oulipo (KMP算法)
Oulipo (KMP算法) 题目链接:HDU-1686 题目: Oulipo Problem Description The French author Georges Perec (1936–19 ...
- F - Oulipo(kmp经典模板题)!!!
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- HDU - 1686 Oulipo KMP
Oulipo Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispari ...
- POJ - Oulipo(KMP)
题目链接:http://poj.org/problem?id=3461 Time Limit: 1000MS Memory Limit: 65536K Description The French a ...
- Oulipo(kmp算法)
题目意思就是找子串在一个长串中出现的次数. 因为数据较大,一般的做法都超时,看了看kmp算法,用这个算法做的.这个算法最难的就是求那个next数组吧. #include<iostream> ...
- POJ3461 Oulipo ——KMP算法——Pku3461
建议大家学一学比较巧妙的KMP算法吧,很有意思.推荐个题目:POJ3167 Cow Patterns 题解我会发在本博里. 这个KMP就木有什么好说的了吧,大家找百度百科学一下就可以了~ CODE P ...
- HDU 1618 Oulipo KMP解决问题的方法
鉴于两个字符串,寻找一个字符串的频率,另一个字符串出现. 原版的kmp另一个陷阱.以下凝视了,标不是踩着好,有加班一定几率,也有机会错误,根据不同的字符串可以是详细. 变化看起来像一个,kmp速度是非 ...
- poj 3461 Oulipo (KMP)
http://poj.org/problem?id=3461 基础KMP, 要注意一次查找完成后,到下一可查找处继续匹配,这样才能保证得到最终个数. code: #include<cstdio& ...
- POJ Oulipo(KMP模板题)
题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...
- 提高篇 第二部分 字符串算法 第1章 哈希和哈希表
浅谈字符串哈希_1264Ikaros的博客-CSDN博客_字符串哈希 图书管理-哈希表_handsome·wjc的博客-CSDN博客 字符串哈希 哈希表 - DTTTTTTT - 博客园 图书管理(L ...
最新文章
- Java 线程数据交换控制器Exchange使用实例
- 【LeetCode 剑指offer刷题】查找与排序题14:Wiggle Sort(系列)
- c语言编写九宫格,关于九宫格的算法
- 攻破 程序员35岁 “瓶颈” 那都不是事!
- 搜索引擎优化学习原理_如何使用数据科学原理来改善您的搜索引擎优化工作
- FGSM对抗样本算法实现
- 数据挖掘与六度分隔理论
- echarts-横坐标文字竖排显示和倾斜45°显示
- macOS Big Sur 安装 CH340 串口转 USB 驱动
- 1Password 7.3.2 强大的密码管理器
- 关于印发《测绘地理信息质量管理办法》的通知
- Java开发 - 树(二叉树,二叉排序树,红黑树)
- ProtoBuf 官方文档翻译
- 2009年必看十大动漫游戏改编电影
- 5、Squid代理服务
- H3C acl配置举例
- AutoHotkey 使用总结
- python tkinter库引用tkk_Python之tkinter:调用python库的tkinter带你进入GUI世界(一)——Jason niu...
- Python的setup模块介绍
- Ansible之 AWX 创建管理项目的一些笔记