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

Oulipo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 50450   Accepted: 20018

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

KMP模板:

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e6+;
const int MAXW = 1e4+;
char W[MAXW], T[MAXN];
int wlen, tlen;
int nxt[MAXW]; void get_Next()
{
int j = , k = -;
nxt[] = -;
while(j < wlen){
if(k == - || W[j] == W[k]){
nxt[++j] = ++k;
}
else k = nxt[k];
}
} int KMP_count()
{
int ans = , j = ;
if(wlen == && tlen == ){
if(W[] == T[]) return ;
else return ;
}
get_Next();
for(int now = ; now < tlen; now++){
while(j > && T[now] != W[j])
j = nxt[j];
if(W[j] == T[now]) j++;
if(j == wlen){
ans++;
j = nxt[j];
}
}
return ans;
} int main()
{
int T_case;
scanf("%d", &T_case);
while(T_case--){
scanf("%s%s", &W, &T);
wlen = strlen(W);
tlen = strlen(T); printf("%d\n", KMP_count());
}
return ;
}

POJ 3461 Oulipo 【KMP统计子串数】的更多相关文章

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

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

  2. POJ 3461 Oulipo KMP

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

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

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

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

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

  5. POJ 3461 Oulipo KMP算法题解

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

  6. POJ 3461 Oulipo(KMP,模式串在主串中出现次数 可重叠)

    题意:给你两个字符串p和s,求出p在s中出现的次数. 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 我先想直接把p接到s前面,之后求Next数组对st ...

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

    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...

  8. POJ 3461 Oulipo(乌力波)

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

  9. poj 3461 Oulipo(kmp统计子串出现次数)

    题意:统计子串出现在主串中的次数 思路:典型kmp #include<iostream> #include<stdio.h> #include<string.h> ...

随机推荐

  1. jquery将日期转换成指定格式的字符串

    引用jquery文件,如<script type="text/javascript" src="jquery-1.8.3.min.js"></ ...

  2. Android耗时操作

    No subscribers registered for event class com.test.MessageEvent import de.greenrobot.event.EventBus; ...

  3. Proguard breaking audio file in assets or raw

    http://stackoverflow.com/questions/21440572/proguard-breaking-audio-file-in-assets-or-raw Issue: I h ...

  4. fastclick.js源码解读分析

    阅读优秀的js插件和库源码,可以加深我们对web开发的理解和提高js能力,本人能力有限,只能粗略读懂一些小型插件,这里带来对fastclick源码的解读,望各位大神不吝指教~! fastclick诞生 ...

  5. 编程中经常看到上下文context,这个上下文指得是什么?

    举个栗子:小美气呼呼对我说:“你去死吧”,我当时哭了. 场景1:小美刚转学到我们学校,我暗恋了她很久,有一天鼓足勇气,向她表白,小美气呼呼对我说:“你去死吧”,我当时就哭了.场景2我跟小美从小青梅竹马 ...

  6. Linux下远程连接工具SSHSecureShellClient的使用

    实际开发中,Linux 服务器都在其他的地方,我们要通过远程的方式去连接 Linux 并操作它,Linux 远程的操作工具有很多,企业中常用的有 Puttty.secureCRT.SSH Secure ...

  7. HDU 4460 Friend Chains

    Problem Description For a group of people, there is an idea that everyone is equals to or less than ...

  8. Enjoy coding

    Enjoy coding iTerm配置 主题选择 Solarized Dark LiquidCarbon 字体选择 Cousine for Powerline(需要安装Powerline字体库), ...

  9. rpm重装python和yum

    前些天升级的python, yum就不能用了, 提示 "No module named yum", 然后搜索了一下, 说要重装python和yum, 也没多想, 就按照那些教程去做 ...

  10. nginx 导致文件上传中途中断 Failed to load resource: net::ERR_CONNECTION_RESET

    昨天上传文件出了问题,常常在进度条到一半的时候就终止了.在本地测试的时候倒是没问题,今天早上用花生壳换了另一个域名,在我本地和服务器都测试,却能够上传文件成功.然后就想到了可能是nginx的问题,也在 ...