原题传送:http://poj.org/problem?id=3461

Oulipo

Time Limit: 1000MS Memory Limit: 65536K

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


KMP模板


#include<stdio.h>
#include<string>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1e4+100;
char s1[maxn],s2[1000100];
int next[maxn]; void cal_next()//获得next数组
{
int k;
k = next[0] = -1;
int len = strlen(s1);
for(int i=1;i<len;i++)
{
while(k>-1 && s1[i] != s1[k+1])
k = next[k];
if(s1[i] == s1[k+1])
k++;
next[i] = k;
}
} int KMP()
{
int ans = 0;
int len = strlen(s2);
int k = -1;
int len1 = strlen(s1);
for(int i=0;i<len;i++)
{
while(k>-1 && s1[k+1] != s2[i])
k = next[k];
if(s1[k+1] == s2[i])
k++;
if(k == len1-1)
{
ans++;
k = next[k];
}
}
return ans;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",s1,s2);
cal_next();
int ans = KMP();
printf("%d\n",ans);
}
return 0;
}

POJ:3461-Oulipo(KMP模板题)的更多相关文章

  1. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  2. POJ Oulipo(KMP模板题)

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

  3. POJ 3641 Oulipo KMP 水题

    http://poj.org/problem?id=3461 直接KMP就好.水题 #include<cstdio> #include<cstring> const int M ...

  4. POJ 3461 Oulipo KMP算法(模板)

    题意: 给两组字符串a和b,求a在b中出现的次数 关于KMP: 马拉车算法是处理回文串,而KMP是处理前后缀的相同字符串的最长长度. a | a | b | a | a | f | a | a 数组 ...

  5. POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用

    题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...

  6. [POJ] 3461 Oulipo [KMP算法]

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23667   Accepted: 9492 Descripti ...

  7. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  8. POJ 3461 Oulipo KMP

    题意:统计其中一个子串的出现次数 题解:即KMP算法中j==m的次数 //作者:1085422276 #include <cstdio> #include <cmath> #i ...

  9. poj3461 Oulipo (KMP模板题~) 前面哪些也是模板题 O.O

    # include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...

  10. POJ 3461 Oulipo KMP算法题解

    本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...

随机推荐

  1. BZOJ1102(搜索)

    随便写一下的搜索,别的OJ深搜就过了,强大的BZOJ成功栈溢出RE了我并使我屈服地用广搜过掉,第一行手动开栈惨遭无视. 广搜: #pragma comment(linker, "/STACK ...

  2. 1-28Map简介

    Map接口概述 除了Collection之外,常用的集合还有Map接口,里面常用的实现类图如下: map中的元素是以键-值的方式存在的,通过键可以获取到值,键是不可以重复的,跟地图比较像,通过一个坐标 ...

  3. 部署iis服务器与c#程序遇到的问题小结

    记得上次部署IIS服务器比较顺利,半天搞定的?有点忘了. 但,服务器版本各有不同,这次装的是server2008 R2 Ennterprice版.虽然忘了上次装的是哪个版本,但进去后发现有些东西明显不 ...

  4. mac Latex dvipdfm 缺少字体错误 Failed to read UCS2/UCS4 TrueType cmap

    dvipdfmx 命令产生 ** ERROR ** Failed to read UCS2/UCS4 TrueType cmap... 错误的原因是没有把 simsun.ttf simkai.ttf ...

  5. 实现一个redis连接池

    #*****************jedis连接参数设置*********************#redis服务器ipredis.ip=169.254.130.122#redis服务器端口号red ...

  6. FastDFS java 辅助类

    package cn.saiz.drkms.task.crack.utils; import java.io.File; import java.io.FileInputStream; import ...

  7. 在Window上用cmd创建.htaccess文件

    Windows 图形下不能直接建立空名字的文件,所以没法直接创建.htaccess文件,不过可以通过命令行创建: cd /path/to/your/dir/ type nul>.htaccess ...

  8. HTML5+CSS3新增内容总结!!!!!绝对干货

    说到H5C3会不会觉得东西好多啊,今天就整理了一份总结性的内容: CSS3选择器有哪些?答:属性选择器.伪类选择器.伪元素选择器.CSS3新特性有哪些?答:1.颜色:新增RGBA,HSLA模式 文字阴 ...

  9. 爬虫基本原理及requests,response详解

    一.爬虫基本原理 1.爬虫是什么 #1.什么是互联网? 互联网是由网络设备(网线,路由器,交换机,防火墙等等)和一台台计算机连接而成,像一张网一样. #2.互联网建立的目的? 互联网的核心价值在于数据 ...

  10. couldn't be opened because you don't have permission to view it” 解决方法

    I use Xcode6 GM. I encountered the same problem. What I did was to go to Build Options. Then I chang ...