码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3461 Oulipo

时间:2017-12-23 11:49:04      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:post   ring   col   出现   ace   scanf   def   printf   space   

Oulipo

 POJ - 3461 

题目大意:有T组数据,每组数据中有两个字符串s1,s2,问s1在s2中出现的次数

/*
    标准kmp模板
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 1000010
using namespace std;
int l1,l2,nxt[maxn];
char s1[maxn],s2[maxn];//s1是短的,s2是长的 
void getnxt(){
    int i=0,j=-1;
    nxt[0]=-1;
    while(i!=l1){
        if(j==-1||s1[i]==s1[j])nxt[++i]=++j;
        else j=nxt[j];
    }
}
int KMP(){
    int i=0,j=0,cnt=0;
    while(i!=l2&&j!=l1){
        if(s2[i]==s1[j]||j==-1)i++,j++;
        else j=nxt[j];
        if(j==l1){
            cnt++;
            j=nxt[j];
        }
    }
    return cnt;
}
int main(){
    int T;scanf("%d",&T);
    while(T--){
        scanf("%s%s",s1,s2);
        l1=strlen(s1);l2=strlen(s2);
        getnxt();
        printf("%d\n",KMP());
    }
    return 0;
}

 

poj 3461 Oulipo

标签:post   ring   col   出现   ace   scanf   def   printf   space   

原文地址:http://www.cnblogs.com/thmyl/p/8092448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!