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

HDU - 2087 剪花布条(kmp)

时间:2017-09-07 10:57:22      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:inline   for   ios   ext   clu   剪花布条   turn   class   cpp   

题意:求w串在T串中的出现次数。(不可重叠出现)

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1000 + 10;
const int MAXT = 1000000 + 10;
using namespace std;
char s1[MAXN], s2[MAXN];
int len1, len2;
int Next[MAXN];
void getNext(){
    Next[0] = 0;
    for(int p = 1, k = 0; p < len2; ++p){
        while(k > 0 && s2[p] != s2[k]) k = Next[k - 1];
        if(s2[p] == s2[k]) ++k;
        Next[p] = k;
    }
}
int kmp(){
    getNext();
    int cnt = 0;
    for(int p = 0, k = 0; p < len1; ++p){
        while(k > 0 && s1[p] != s2[k]) k = Next[k - 1];
        if(s1[p] == s2[k]) ++k;
        if(k == len2){
            ++cnt;
            k = 0;
        }
    }
    return cnt;
}
int main(){
    while(scanf("%s", s1) == 1){
        if(strcmp(s1, "#") == 0) return 0;
        scanf("%s", s2);
        len1 = strlen(s1);
        len2 = strlen(s2);
        printf("%d\n", kmp());
    }
    return 0;
}

  

HDU - 2087 剪花布条(kmp)

标签:inline   for   ios   ext   clu   剪花布条   turn   class   cpp   

原文地址:http://www.cnblogs.com/tyty-Somnuspoppy/p/7488473.html

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