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. Hadoop自学笔记(二)HDFS简单介绍
  2. python3 base64 长度补全
  3. 哪些云计算企业能活下来
  4. torchvision.transforms包的使用
  5. L 1 ,L 2 参数正则化
  6. wso2 安装_WSO2注册表安装简介
  7. 判断一个整数是不是回文
  8. 如何使用JavaScript来写ASP程序
  9. 计算机识别人脸原理,深入浅出人脸识别原理
  10. 天地不仁,以万物为刍狗
  11. 仿酒仙网品牌活动动画效果 (鼠标移上 图片平移)
  12. 软件工程师——计算机组成原理
  13. win10网络连接为地球禁用标志解决办法
  14. 【电脑使用】利用diskpart删除电脑的EFI分区
  15. 爬虫写得好,牢饭吃得早
  16. 中国半导体企业IPO激增;英特尔拆分显卡业务部门;特斯拉将开启新一轮裁员丨每日大事件...
  17. 13个高含金量编程竞赛,99%的家长都为孩子收藏了
  18. 大数据行业怎么样?未来发展好么?
  19. 2020 CSP-游记
  20. 关于AP2331SA-7

热门文章

  1. (附源码)计算机毕业设计ssm高校请假管理系统
  2. 证券从业考试备考经验1115
  3. 酬乐天扬州初逢席上见赠
  4. 【科普】Windows客户端加域管理(AD)
  5. 从本地管理员到域管理员提权
  6. oracle如何异地备份软件,数据库Oracle数据的异地的自动备份
  7. office卸载重新安装,并安装mathtype7数学编辑公式
  8. Labview串口通信中ASCII码和数值相互转换
  9. linux中 777,755等用户权限说明
  10. python中有没有switch_为什么python没有switch/case