题目链接:http://poj.org/problem?id=3461

Oulipo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 49608   Accepted: 19699

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

Source

 
题目大意:输入t,代表t组样例,每个样例有两个字符串,求第一个字符串在第二个字符串中出现的次数
思路:可以说是kmp的裸题,注意的是kmp好多题目都卡cin,都要用scanf才不会超时
看代码:
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=;
const int maxn=1e6+;
const int maxk=5e3+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int next[maxn];
void cal_next(char a[])
{
int len=strlen(a);
next[]=-;
int k=-;
for(int i=;i<len;i++)
{
while(k>-&&a[k+]!=a[i])
{
k=next[k];
}
if(a[k+]==a[i]) k++;
next[i]=k;
}
}
void kmp(char a[],char b[])
{
int k=-,sum=;
int len1=strlen(a);
int len2=strlen(b);
for(int i=;i<len2;i++)
{
while(k>-&&a[k+]!=b[i])
{
k=next[k];
}
if(a[k+]==b[i]) k++;
if(k==len1-)
{
sum++;
k=next[k];
}
}
printf("%d\n",sum);
//cout<<sum<<endl;
}
int main()
{
//string a,b;
char a[maxn],b[maxn];
int t;
//cin>>t;
scanf("%d",&t);
while(t--)
{
//cin>>a>>b;
scanf("%s",a);
getchar();
scanf("%s",b);
cal_next(a);
kmp(a,b);
}
return ;
}

POJ - 3461 (kmp)的更多相关文章

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

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

  2. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

  3. POJ 2406 Power Strings(KMP)

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  4. POJ题目(转)

    http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj29 ...

  5. LightOJ 1258 Making Huge Palindromes(KMP)

    题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...

  6. codeM编程大赛E题 (暴力+字符串匹配(kmp))

    题目大意:S(n,k)用k(2-16)进制表示1-n的数字所组成的字符串,例如S(16,16)=123456789ABCDEF10: 解题思路: n最大50000,k最大100000,以为暴力会超时. ...

  7. 经典串匹配算法(KMP)解析

    一.问题重述 现有字符串S1,求S1中与字符串S2完全匹配的部分,例如: S1 = "ababaababc" S2 = "ababc" 那么得到匹配的结果是5( ...

  8. URAL 1732 Ministry of Truth(KMP)

    Description In whiteblack on blackwhite is written the utterance that has been censored by the Minis ...

  9. Leetcode28--->字符串的匹配(KMP)

    题目: 题目的本质是给定两个字符串str1,str2,求str1中的str2串开始的地方,即字符串的匹配,KMP算法 思路:时间复杂度为O(m + n),空间复杂度为O(n),原串的长度为m,子串的长 ...

随机推荐

  1. bzoj 2716 [Violet 3]天使玩偶——KDtree

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2716 第三道KDtree!仍旧是模板.还有CDQ分治做法,见下面. 数组迷之开大?(开6e5 ...

  2. bzoj 5120 无限之环 & 洛谷 P4003 —— 费用流(多路增广SPFA)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5120 https://www.luogu.org/problemnew/show/P4003 ...

  3. 杂项-权限管理:Spring Secutity

    ylbtech-杂项-权限管理:Spring Secutity Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在S ...

  4. [置顶] 什么是C语言结构体字节对齐,为什么要对齐?

    一.概念 对齐跟数据在内存中的位置有关.如果一个变量的内存地址正好位于它长度的整数倍,他就被称做自然对齐.比如在32位cpu下,假设一个整型变量的地址为0x00000004,那它就是自然对齐的.   ...

  5. 基于STM32的三轴数字罗盘HMC5883L模块的测试

    最近买了个数字罗盘模块,调通后发现很不错,非常灵敏,测试的时候精度在1°以内.连续测量模式下,最快测量.输出速率可达75hz,模块每次测量完毕并将数据更新至寄存器后,其DRDY引脚便产生一个低电平脉冲 ...

  6. redis-数据类型-string、hash、list、set、zset

    String 类型操作string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象. $redis-> ...

  7. MySql数据库数据更新操作其高级应用

    数据更新操作有3种:向表中添加数据.修改表中的数据和删除表中的数据. 用来演示的数据表用student.course.sc三个数据表,数据表具体内容在:PHP和MySql数据库,如何获取每个分类的记录 ...

  8. ubuntu 下minicom超级终端的使用方法

    http://blog.chinaunix.net/uid-25909619-id-3184639.html Ubuntu下使用sshfs挂载远程目录到本地 http://blog.csdn.net/ ...

  9. SQLServer数据库权限设置--保障数据库安全

    一.登陆界面引入 下图为SQL Server的登陆界面. 1)服务器名称:“.”代表本地计算机,选择下拉框,可以看见还有一个与本机机名相同的内容,也代表于本地服务器连接:要连接远程服务器的话,在此处填 ...

  10. Jquery选择器(三)

    过滤选择器 4.属性过滤器 查找所有含有 id 属性的 div 元素$(document).ready(function(){ $("div[id]").css("col ...