Oulipo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28155   Accepted: 11251

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 textT,
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模板题,没有比这个更模板的了。。。

唯一要提醒自己的是最后模式串和原串走的时候j是一直往前走的,不回头!怎么就记不住,每次都是搞了两层循环,然后TLE,你这么搞之前搞的那个next数组有什么用,动脑袋想一想啊光速小子。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
using namespace std; char moshi[10005];
char pipei[1000005]; int result;
int next_moshi[10005]; void get_next()
{
int i,j=-1;
int len=strlen(moshi);
next_moshi[0]=-1; for(i=0;i<len;)
{
if(j==-1||moshi[i]==moshi[j])
{
i++;
j++; if(moshi[i]==moshi[j])
{
next_moshi[i]=next_moshi[j];
}
else
{
next_moshi[i]=j;
}
}
else
{
j=next_moshi[j];
}
}
} void get_result()
{
int i=0,j=0; int len_pipei=strlen(pipei);
int len_moshi=strlen(moshi); while(j<len_pipei)
{
if(moshi[i]==pipei[j])
{
i++;
j++;
}
else
{
if(next_moshi[i]==-1)
{
i=0;
j++;
}
else
{
i=next_moshi[i];
}
}
if(i>=len_moshi)
{
result++;
i=next_moshi[len_moshi];
}
}
} int main()
{
int Test;
scanf("%d",&Test); while(Test--)
{
result=0; scanf("%s%s",moshi,pipei); get_next();
get_result(); cout<<result<<endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3461:Oulipo的更多相关文章

  1. 【poj 3461】Oulipo(字符串--KMP)

    题意:求子串在文本串中出现了多少次. 解法:使用KMP的next[ ]和tend[ ]数组计数. #include<cstdio> #include<cstdlib> #inc ...

  2. 【POJ 3461】 Oulipo

    [题目链接] 点击打开链接 [算法] KMP [代码] #include <algorithm> #include <bitset> #include <cctype&g ...

  3. POJ 3461 Oulipo

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

  4. POJ 3461 Oulipo(乌力波)

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

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

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

  6. 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 ...

  7. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  8. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  9. POJ 3461 Oulipo 【KMP统计子串数】

    传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...

随机推荐

  1. mysql#自定义序列

    原文 mysql主键不用自增数字的时候,可以参考如下方式,我抄来的. -- 创建公共的序列表 DROP TABLE IF EXISTS t_common_sequence; CREATE TABLE ...

  2. matlab练习程序(概率路线图PRM)

    PRM概率路线图全称 Probabilistic Roadmap,是一种路径规划算法,利用随机撒点的方式将空间抽样并将问题转为图搜索,利用A*或Dijkstra算法找到起始结束节点的最短路径. 可以想 ...

  3. MVC5仓库管理系统

    下载

  4. 【转】WdatePicker日历控件使用方法

    转                自:   https://www.cnblogs.com/yuhanzhong/archive/2011/08/10/2133276.html WdatePicke官 ...

  5. SPFA和堆优化的Dijk

    朴素dijkstra时间复杂度$O(n^{2})$,通过使用堆来优化松弛过程可以使时间复杂度降到O((m+n)logn):dijkstra不能用于有负权边的情况,此时应使用SPFA,两者写法相似. 朴 ...

  6. C++面试常见问题——17类模板的使用

    类模板的使用 注意在每次类模板函数时都需要声明一个类模板 #include<iostream> using namespace std; template <class T,int ...

  7. java随机函数用法Random

     原文地址:http://blog.csdn.net/wpjava/article/details/6004492  import java.util.Random; public class Ran ...

  8. 2020寒假 05 ——eclipse安装scala环境

    在eclipse中安装Scala环境 1安装eclipse插件步骤,点击help,选择Eclipse Marketplace 2.输入Scala,点击go 3.选择搜索到的Scala IDE 4.7. ...

  9. 061、Java中利用return结束方法调用

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  10. 058、Java中定义一个没有参数没有返回值的方法

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...