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 long
#define FOR(i,x,y) for(int i=x;i<=y;i++)
using namespace std;
const int maxn = 1000000 + 10;
char s[maxn], t[maxn];
int next[maxn];
void get_next()
{
    int m = strlen(s);
    next[0] = 0; next[1] = 0;
    for(int i=1;i<m;i++)
    {
        int j = next[i];
        while(j && s[i] != s[j]) j = next[j];
        next[i+1] = (s[i] == s[j]) ? j + 1: 0;
    }
}
int KMP()
{
    int n = strlen(t), m = strlen(s);
    get_next();
    int j = 0, cnt = 0;
    for(int i=0;i<n;i++)
    {
        while(j && s[j] != t[i]) j = next[j];
        if(s[j] == t[i]) j++;
        if(j == m) cnt++;
    }
    return cnt;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%s%s", &s, &t);
        int ans = KMP();
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-11-03 05:43:55

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算法问题

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

POJ 3461 Oulipo KMP算法题解

本题就是给出很多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道几乎一模一样的题目. 使用KMP算法加速,算法高手必会的算法了. 另外看见讨论说什么使用KMP还超时,最大可能是没有真正理解next table的含义,写了错误的代码,故此虽然自己运行结果正确,但是却没有真正发挥next table的作用,使得算法退化为暴力法了,所以运行正确,但超时. KMP参考: http://blog.csdn.net/kenden23/articl

[POJ] 3461 Oulipo [KMP算法]

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

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

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

POJ 3461 Oulipo (求模式串在文本串中出现的次数)

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

E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3461 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member