Problem 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

题目大意:

求在父串中有几个目标串,注意:在父串中上一个与目标串相同的子串可以和下一个与目标串相等的子串共用字符。

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相关推荐

  1. Oulipo (KMP算法)

    Oulipo (KMP算法) 题目链接:HDU-1686 题目: Oulipo Problem Description The French author Georges Perec (1936–19 ...

  2. F - Oulipo(kmp经典模板题)!!!

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  3. HDU - 1686 Oulipo KMP

    Oulipo Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispari ...

  4. POJ - Oulipo(KMP)

    题目链接:http://poj.org/problem?id=3461 Time Limit: 1000MS Memory Limit: 65536K Description The French a ...

  5. Oulipo(kmp算法)

    题目意思就是找子串在一个长串中出现的次数.  因为数据较大,一般的做法都超时,看了看kmp算法,用这个算法做的.这个算法最难的就是求那个next数组吧. #include<iostream> ...

  6. POJ3461 Oulipo ——KMP算法——Pku3461

    建议大家学一学比较巧妙的KMP算法吧,很有意思.推荐个题目:POJ3167 Cow Patterns 题解我会发在本博里. 这个KMP就木有什么好说的了吧,大家找百度百科学一下就可以了~ CODE P ...

  7. HDU 1618 Oulipo KMP解决问题的方法

    鉴于两个字符串,寻找一个字符串的频率,另一个字符串出现. 原版的kmp另一个陷阱.以下凝视了,标不是踩着好,有加班一定几率,也有机会错误,根据不同的字符串可以是详细. 变化看起来像一个,kmp速度是非 ...

  8. poj 3461 Oulipo (KMP)

    http://poj.org/problem?id=3461 基础KMP, 要注意一次查找完成后,到下一可查找处继续匹配,这样才能保证得到最终个数. code: #include<cstdio& ...

  9. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

  10. 提高篇 第二部分 字符串算法 第1章 哈希和哈希表

    浅谈字符串哈希_1264Ikaros的博客-CSDN博客_字符串哈希 图书管理-哈希表_handsome·wjc的博客-CSDN博客 字符串哈希 哈希表 - DTTTTTTT - 博客园 图书管理(L ...

最新文章

  1. HTML5编程之旅 第4站 Forms
  2. LLDB+Python脚本:增强LLDB调试
  3. php 浅复制 和 深复制(clone)
  4. python cookbook pdf下载-Python Cookbook 第3版 中文版.pdf
  5. 在sublime-text中设置浏览器预览
  6. 华为发布The New P30 Pro新机:全新配色+EMUI10系统
  7. C语言 用传统流程图表示分段函数,C语言程序设计习题答案
  8. 接口测试文件上传(python+requests)
  9. html的那些小小细节
  10. enote笔记语言(2)(ver0.2)
  11. CF468C Hack It! 构造
  12. win10应用商店打不开_最冷清的应用商店?为何Win10商店无人问津
  13. 使用java 实现 word 转换成图片
  14. 服务器获取交换机端口状态,如何查找交换机端口对应的IP地址
  15. flask+pytorch部署深度学习(图像识别)项目
  16. 04_Mybatis输入\出映射
  17. python实时监控电脑运行信息(邮件预警)
  18. win10右键卡死/导航栏点击文件夹图标无法弹出资源管理器/右键cmd以管理员身份运行无反应
  19. java utf-8_java如何把string转为utf-8
  20. 《商业的本质》读后感

热门文章

  1. 硬笔书法“案”字怎样书写正确?“最美中国字”来教你!
  2. 【原理分析】Google炫炸天的平衡自行车仅仅是概念吗?来看看惯性轮自行车吧...
  3. ahk写入excel单元格_输出excel数据到GUI 获取excel所有Sheet及字段 Autohotkey
  4. 最新有效的微视频号视频下载丨vx视频号视频怎么免费下载?
  5. 大专生三面蚂蚁金服,工信部java工程师证书
  6. 具有系统权限的apk的安装和系统签名
  7. [机缘参悟-28]:鬼谷子-内揵篇-保全自己,说服上司
  8. Linux三大剑客之sed
  9. Innovus update_io_latency
  10. 手把手教你用 Python 爬虫煎蛋妹纸海量图片