zoukankan      html  css  js  c++  java
  • HDU 1358 简单kmp

     题目大意:

    找到所有的可组成连续字符串相连的位置,和循环字符串的个数

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <climits>
    #include <cmath>
    #include <cstdlib>
    
    using namespace std;
    
    #define ll long long
    #define N 1000100
    char s[N] , b[N];
    int _next[N] , len1 , len2;
    
    void get_next(int n)
    {
        _next[0] = _next[1] = 0;
        for(int i=1 ; i<n ; i++){
            int j = _next[i];
            while(j&&b[i]!=b[j]) j=_next[j];
            if(b[i]==b[j]) _next[i+1] = j+1;
            else _next[i+1] = 0;
        }
    }
    
    int kmp(int n)
    {
        int ret = 0;
        int i , j=0;
        for(i=0 ; i<n ; i++){
            if(s[i]==b[j]) j++;
            else{
                while(j&&s[i]!=b[j]) j=_next[j];
                if(!j && s[i]!=b[j]) j=0;
                else j++;
            }
            if(j==len2) ret++ , j=0;
        }
        return ret;
    }
    
    int main()
    {
        #ifndef ONLINE_JUDGE
            freopen("a.in" , "r" , stdin);
        #endif
        while(scanf("%s" , s) , s[0]!='#')
        {
            scanf("%s" , b);
            len1 = strlen(s) , len2 = strlen(b);
            get_next(len2);
            printf("%d
    " , kmp(len1));
        }
        return 0;
    }
  • 相关阅读:
    linux之awk命令
    HDU 2097 Sky数 进制转换
    HDU 2077 汉诺塔IV
    HDU 2094 产生冠军 dfs加map容器
    HDU 2073 叠框
    HDU 2083 简易版之最短距离
    HDU 2063 过山车 二分匹配
    天梯 1014 装箱问题
    天梯 1214 线段覆盖
    天梯 1098 均分纸牌
  • 原文地址:https://www.cnblogs.com/CSU3901130321/p/4599011.html
Copyright © 2011-2022 走看看