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]==t[k]){
            ++j;
            ++k;
            next[j]=k;//此句可由优化替代
            /*优化(仅保证求KMPIndex时可用。谨慎使用。)
            if(t[j]!=t[k])next[j]=k;
            else next[j]=next[k];
            */
        }
        else k=next[k];
    }
}

int KMPIndex(char s[],char t[]){//求子串首次出现在主串中的位置
    int i,j,lens,lent;
    i=j=0;
    lens=strlen(s);
    lent=strlen(t);

    while(i<lens&&j<lent){
        if(j==-1||s[i]==t[j]){
            ++i;
            ++j;
        }
        else j=next[j];
    }
    if(j>=lent)return i-lent;
    else return -1;
}

int KMPCount(char s[],char t[]){//统计子串在主串中的出现次数,可重叠
    int i,j,lens,lent,cnt;
    i=j=0;
    lens=strlen(s);
    lent=strlen(t);
    cnt=0;

    while(i<lens){
        if(j==-1||s[i]==t[j]){
            ++i;
            ++j;
        }
        else j=next[j];
        if(j==lent)++cnt;
    }
    return cnt;
}

void KMPCount2(char t[]){//统计单串中从某个位置以前有多少重复的串
    int i,lent,tmp;
    lent=strlen(t);

    for(i=2;i<=lent;++i){
        tmp=i-next[i];
        if(i%tmp==0&&i/tmp>1)
            printf("\t位置:%d 个数:%d\n",i,i/tmp);
    }
}

int main(){
    char str1[1000005],str2[10005];
    int i;
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s%s",str2,str1);
        GetNext(str2);//求子串的next数组
        printf("%d\n",KMPCount(str1,str2));
    }
    return 0;
}

时间: 2024-12-06 05:52:19

poj 3461 Oulipo(kmp统计子串出现次数)的相关文章

[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 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: 26252   Accepted: 10478 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 &amp; KMP模板

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: Tout avait Pair normal, mai

POJ 3461 Oulipo kmp 水过

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29178   Accepted: 11690 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 kmp字符串匹配

//#include <iostream> #include <stdio.h> #include <string.h> using namespace std; //string a,b; char a[10000],b[1000000]; int asize,bsize; int kmp(){ int *pi = new int [asize]; pi[0] = -1; for(int i = 1,k = -1;i<asize;i++){ while(k>

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 (求模式串在文本串中出现的次数)

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

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