POJ3461 Oulipo KMP算法应用

题目描述

给定主串和模式串,问模式串在主串中出现的次数

Sample Input

3

BAPC

BAPC

AZA

AZAZAZA

VERDI

AVERDXIVYERDIAN

Sample Output

1

3

0

解题思路:KMP算法是找到一个匹配就跳出,这题是要计数,所以我们把KMP算法稍微改一下即可,在找到一个匹配(即j=模式串长度)时计数器++,再从next[j]开始找就好了。详见代码

#include <cstdio>
#include <cstring>
void GetNext(char* p,int next[])
{
    int pLen = strlen(p);
    int k = -1;//k记录的是next[j]
    next[0] = k;
    int j = 0;
    while (j < pLen) {
        /** next[j]=-1时,next[j+1]肯定是0;p[j]=p[k]时,next[j+1]=next[j]+1 */
        if (k == -1 || p[j] == p[k]) {
            ++k;
            ++j;
            if(p[j] != p[k]) next[j] = k;
            else next[j] = next[k];
        }
        else k = next[k];
    }
}
const int maxn = 1000010;
char s[maxn];
char p[10010];
int next[10010];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%s",p);
        GetNext(p,next);
        int cnt = 0;
        scanf("%s",s);
        int i=0,j=0;
        int len1=strlen(s),len2=strlen(p);
        while(i < len1) {
            if(j == -1 || s[i] == p[j]) {
                ++i;
                ++j;
                if(j == len2) {  //找到了一个匹配串
                    cnt++;       //计数器++
                    j = next[j]; //从next[j]继续开始匹配
                }
            }else j = next[j];
        }
        printf("%d\n",cnt);
    }
    return 0;
}
时间: 2024-10-15 12:24:24

POJ3461 Oulipo KMP算法应用的相关文章

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

hdoj 1686 Oulipo(KMP算法)

Oulipo http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5903    Accepted Submission(s): 2370 Problem Description The French author Georges Perec (1

POJ3461 Oulipo[KMP]【学习笔记】

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

hdu 1686 Oulipo kmp算法

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: 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

poj3461 Oulipo (kmp)

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

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

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; char a1[1000010],a2[1000010]; int next[1000010]; int len1,len2,cot; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=len1) { if(j==-1||a1[i]==a1[j]

KMP算法之Oulipo

Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8137    Accepted Submission(s): 3280 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, wit

POJ3461 Oulipo 【KMP】

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