POJ-3461 Oulipo(KMP)

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

题目大意:

给你两个字符串p和s,求出p在s中出现的次数。

思路:p在s中KMP匹配,匹配成功,再从next[last]的位置匹配即可,因为允许出现的两次有重叠的部分。

//1208 KB	94 ms
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n;
char s[1000100],p[10100];
int next[10100];
void getnext()
{
    next[0]=-1;
    int i=0,j=-1;
    while(p[i]!=0){
        while(j>-1&&p[i]!=p[j])
            j=next[j];
        j++;
        i++;
        next[i]=j;
    }
}
int KMP()
{
    int i=0,j=0,ans=0;
    while(s[i]!=0){
        if(j==-1||s[i]==p[j]){
            j++;
            i++;
            if(p[j]==0){
                ans++;
                j=next[j];
            }
        }
        else j=next[j];
    }
    return ans;
}
int main()
{
    int _;
    scanf("%d",&_);
    while(_--){
        scanf("%s%s",p,s);
        getnext();
        printf("%d\n",KMP());
    }
    return 0;
}
时间: 2024-10-22 10:32:51

POJ-3461 Oulipo(KMP)的相关文章

poj 3461 Oulipo(KMP模板题)

题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23559   Accepted: 9437 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a

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

题意:统计子串出现在主串中的次数 思路:典型kmp #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int next[10005]; void GetNext(char t[]){//求next数组 int j,k,len; j=0; k=-1; next[0]=-1; len=strlen(t); while(j<len){ if(k==-1||t[j]==

POJ 题目3461 Oulipo(KMP)

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26479   Accepted: 10550 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 quot

POJ 3461 Oulipo(乌力波)

POJ 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: Tou

POJ 3461 Oulipo(自己YY的模式匹配算法)

请不要随便指点别人该怎么做.每个人的人生都应该自己掌握.你给不了别人一切.你也不懂别人的忧伤. 微笑不代表快乐.哭泣不一定悲伤 不努力怎么让关心你的人幸福.不努力怎么让看不起你的人绝望. 我用生命在奋斗--lx_Zz ------------------------------------------------------------- -------------------------    华丽的分割线    ---------------------------- -----------

hdu 1686 Oulipo (kmp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:寻找子链在母链中出现的次数. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int next[10010],sum,lens,lenc; 6 char str[10010],ch[1000010]; 7 8 int get_nex

poj 3461 - Oulipo 经典kmp算法问题

2017-08-13 19:31:47 writer:pprp 对kmp算法有了大概的了解以后,虽然还不够深入,但是已经可以写出来代码,(可以说是背会了) 所以这道题就作为一个模板,为大家使用吧. 题目大意:给你一个子串P和一个主串S,求在主串中有多少个子串? 代码如下:(需要注意的点我都标记好了,两个函数可以直接用) #include <iostream> #include <cstdio> #include <cstdlib> #include <cstrin

HDU 1686 Oulipo(kmp)

Problem 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

POJ 3461 Oulipo(简单KMP)

解题思路: 典型KMP,直接搞. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <queue> #include <stack> #define LL long lo