Hdoj 1686 Oulipo

/*
Problem :
Status  :

By wf,
*/

#include "algorithm"
#include "iostream"
#include "cstring"
#include "cstdio"
#include "string"
#include "stack"
#include "cmath"
#include "queue"
#include "set"
#include "map"

#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1

typedef long long ll;
using namespace std;

const int inf=0x3f3f3f3f;
const int maxn=1e6+5;

char sub[10000+5];
char str[maxn];

//刘汝佳
int f[10000+5];
void getFail(char* P,int * f)
{
    int m = strlen(P);
    f[0]=0;f[1]=0;
    for(int i=1;i<m;++i)
    {
        int j = f[i];
        while(j && P[i]!=P[j])j=f[j];
        f[i+1] = P[i]==P[j] ? j+1 :0;
    }
}

void kmp(char*T,char * P,int *f)
{
    int ret = 0;
    int n = strlen(T),m = strlen(P);
    getFail(P,f);
    int j=0;
    for(int i=0;i<n;++i)
    {
        while(j && P[j]!=T[i]) j = f[j];
        if(P[j]==T[i])j++;
        if(j==m)
        {
            ret++;
            //printf("%d\n",i-m+1);
        }

    }
    printf("%d\n",ret);
}

/*
kuangbin
void getFail(char P[],int f[])
{
    int m = strlen(P);
    int i=0,j=-1;
    f[0]=-1;
    while(i<m)
    {
       while(-1!=j && P[i]!=P[j])j=f[j];
       f[++i]=++j;
    }
    for(int i=0;i<m;++i)printf("%d ",f[i]);
    printf("\n");
}

void kmp(char T[],char P[],int f[])
{
    int ret = 0;
    int n = strlen(T),m = strlen(P);
    //printf("n==%d , m==%d\n",n,m);
    getFail(P,f);
    int i=0,j=0;
    while(i<n )
    {
        while(-1!=j && T[i]!=P[j])j=f[j];
        ++i;++j;
        if(j>=m)
        {
            ret++;
            j=f[j];
        }
    }
    printf("ret==%d\n",ret);
}
*/

int main()
{
    //freopen("in.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",&sub);
        scanf("%s",&str);
        //printf("%s\n",sub);
        //printf("%s\n",str);
        kmp(str,sub,f);
    }

    return 0;
}

  

时间: 2024-10-09 04:56:03

Hdoj 1686 Oulipo的相关文章

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

hdoj 1686 Oulipo【求一个字符串在另一个字符串中出现次数】

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

hdoj 1686 Oulipo【kmp】

Oulipo Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 80   Accepted Submission(s) : 51 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without

【HDOJ】1686 Oulipo

kmp算法. 1 #include <cstdio> 2 #include <cstring> 3 4 char src[10005], des[1000005]; 5 int next[10005], total; 6 7 void kmp(char des[], char src[]){ 8 int ld = strlen(des); 9 int ls = strlen(src); 10 int i, j; 11 12 total = i = j = 0; 13 while (

HDU - 1686 Oulipo KMP匹配运用

HDU - 1686 Oulipo Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description The French author Georges Perec (1936?1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Ouli

HDU 1686 Oulipo 求大串中最多可匹配多少个小串(kmp)

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

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

hdoj 1686 kmp

题目: Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN Sample Output 1 3 0 代码: #include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn1=1000010;const int maxn2=10010;int n,m;char a[maxn1];char b[maxn

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