POJ-3461 Oulipo(KMP,模式串在主串中出现次数)

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

  我先想直接把p接到s前面,之后求Next数组对strlen(p)取余==0的就可以,之后WA。最后发现A AASSAAS的时候有bug,只有又想到在p和s中间加个不可能出现的字符‘$‘就可以了,戒指就A了。

#include <bits/stdc++.h>
using namespace std;

const int INF=0x3f3f3f3f;
typedef long long ll;
#define PI(A) printf("%d\n",A)
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
const double EPS= 1e-9 ;

/*  /////////////////////////     C o d i n g  S p a c e     /////////////////////////  */

const int MAXN= 10000+ 1000000+9 ;

void kmp_pre(int x[],int m,int Next[])
{
    int i,j;
    j=Next[0]=-1;
    i=0;
    while(i<m)
    {
        while(-1!=j&&x[i]!=x[j])j=Next[j];
        Next[++i]=++j;
    }
}
int Next[MAXN],inp[MAXN];
int N,M;
char str1[10000+5],str2[1000000+5];
int main()
{

    int o;
    SI(o);
    while(o--)
    {
        scanf("%s",str1);
        scanf("%s",str2);
        N=strlen(str1),M=strlen(str2);
        str1[N]=‘$‘;
        N++;
        for (int i=0;i<N;i++)
            inp[i]=str1[i];
        for (int i=N;i<N+M;i++)
            inp[i]=str2[i-N];
        kmp_pre(inp,N+M,Next);
        int ans=0;
        Rep(i,1,N+M) if (Next[i]%(N-1)==0&&Next[i]!=0) ans++;
        PI(ans);
    }

    return 0;
}
时间: 2024-10-12 15:18:31

POJ-3461 Oulipo(KMP,模式串在主串中出现次数)的相关文章

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 [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 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 &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字符串匹配

//#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(自己YY的模式匹配算法)

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

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(乌力波)

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