HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)

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





Http

HDU:https://vjudge.net/problem/HDU-1686

POJ:https://vjudge.net/problem/POJ-3461

SCU:https://vjudge.net/problem/SCU-2652

Source

字符串匹配,KMP

解决思路

直接运用KMP算法解题,请看我的这篇文章

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std; const int maxN=10001;
const int maxM=1000001;
const int inf=2147483647; int n,m;
char W[maxN];
char T[maxM];
int F[maxN]; int main()
{
int TT;
cin>>TT;
for (int ti=1;ti<=TT;ti++)
{
scanf("%s",W);
scanf("%s",T);
n=strlen(W);
m=strlen(T);
F[0]=-1;
for (int i=1;i<n;i++)
{
int j=F[i-1];
while ((W[j+1]!=W[i])&&(j!=-1))
j=F[j];
if (W[j+1]==W[i])
F[i]=j+1;
else
F[i]=-1;
}
int i=0,j=0;
int Ans=0;
while (i<m)
{
if (W[j]==T[i])
{
i++;
j++;
if (j==n)
{
Ans++;
j=F[j-1]+1;
}
}
else
if (j==0)
i++;
else
j=F[j-1]+1;
}
cout<<Ans<<endl;
}
return 0;
}

HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)的更多相关文章

  1. (KMP)Oulipo -- poj --3461

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=92486#problem/B http://poj.org/problem?id=3461 ...

  2. Oulipo POJ - 3461(kmp,求重叠匹配个数)

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

  3. Match:Oulipo(POJ 3461)

     Oulipo 题目大意:给你一个字符串,要你找到字符串包含指定子串的个数 只要你知道了KMP,这一题简直不要太简单,注意STL的string是会超时的,还是乖乖用char吧 #include < ...

  4. AC日记——Oulipo poj 3461

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37958   Accepted: 15282 Description The ...

  5. POJ 3267:The Cow Lexicon 字符串匹配dp

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 4228 D ...

  6. POJ 3461 Oulipo

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  7. POJ 3461 Oulipo(乌力波)

    POJ 3461 Oulipo(乌力波) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] The French autho ...

  8. hdu 1686 Oulipo KMP匹配次数统计

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...

  9. POJ 3461 Oulipo[附KMP算法详细流程讲解]

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

随机推荐

  1. xlutils模块使用

    python常用模块目录 1.xlutils 实现拷贝原文件 原表格: import xlrd from xlutils.copy import copy workbook = xlrd.open_w ...

  2. route命令详情

    基础命令学习目录首页 原文链接:https://www.cnblogs.com/lpfuture/p/5857738.html 考试题一:linux下如何添加路由(百度面试题) 以上是原题,老男孩老师 ...

  3. css3 transform属性多值的顺序问题

    对于transform属性的多值的顺序问题,我自己就被困扰过.后来知道了跟顺序有关,但是不知道为什么.我想应该很多人跟我以前一样,知其然不知其所以然.如果不知道的,也许这篇文章会对大家有所帮助. 先来 ...

  4. Scrum立会报告+燃尽图(十二月八日总第三十九次):制定视频方案

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...

  5. 软件工程-东北师大站-第十次作业(PSP)

    1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图

  6. java的第二个实验——JAVA面向对象程序设计

    java的第二个实验——JAVA面向对象程序设计 北京电子科技学院 实     验    报     告 课程:Java程序设计 班级:1352 姓名:林涵锦 学号:20135213 成绩:      ...

  7. 20172325 2016-2017-2 《Java程序设计》第四周学习总结

    20172325 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 1.对类.对象.声明变量的定义和属性有了进一步的了解 2.学会如何编写一个类并运用到需要的程 ...

  8. c# 程序重启设定

    问题情境: 程序随着时间运行,越来越大.暂时想到的两种方法,一是反攻代码,查看占内存大的函数,是不是没有回收.再就是暴力设定程序定时重启. 解决原理: 定时重启:暂设定timer,时间匹配执行rest ...

  9. Structs2笔记①--structs的背景、structs2框架的意义、第一个helloworld

    Struts2的背景 由出色稳定的框架struts1和webwork框架整合而来的 吸取了两大框架的优点 提高了开发的效率和规范性 更好的实现了MVC架构 解除了与servlet的强耦合性 使用str ...

  10. 评论各组alpha发布

    单纯从用户和体验者的角度来评价. 天天向上组的连连看游戏和新锋组的俄罗斯方块游戏,从alpha发布的成果完成度来看,两个游戏现在都可以玩,但连连看的完成度更高,可选背景,可选音乐.俄罗斯方块还有其他界 ...