zoukankan      html  css  js  c++  java
  • Luogu4391 [BOI2009]Radio Transmission 无线传输 (KMP)

    (最小循环节) (=) (lenghth - next[lenghth])

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
    #define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
    #define Max(a,b) ((a) > (b) ? (a) : (b))
    #define Min(a,b) ((a) < (b) ? (a) : (b))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Abs(a) ((a) < 0 ? -(a) : (a))
    #define Swap(a,b) a^=b^=a^=b
    #define ll long long
    
    #define ON_DEBUG
    
    #ifdef ON_DEBUG
    
    #define D_e_Line printf("
    
    ----------
    
    ")
    #define D_e(x)  cout << #x << " = " << x << endl
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt","r",stdin);
    
    #else
    
    #define D_e_Line ;
    #define D_e(x)  ;
    #define Pause() ;
    #define FileOpen() ;
    
    #endif
    
    struct ios{
        template<typename ATP>ios& operator >> (ATP &x){
            x = 0; int f = 1; char c;
            for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
            while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
            x*= f;
            return *this;
        }
    }io;
    using namespace std;
    
    int nxt[1000007];
    char str[1000007];
    int main(){
    	int len;
    	io >> len;
    	scanf("%s", str);
    	int i = 0, j = -1;
    	nxt[0] = -1;
    	while(i < len){
    		if(j == -1 || str[i] == str[j])
    			nxt[++i] = ++j;
    		else
    			j = nxt[j];
    	}
    	
    	printf("%d", len - nxt[len]);
    	return 0;
    } 
    

  • 相关阅读:
    BZOJ3847 : ZCC loves march
    BZOJ3828 : [Poi2014]Criminals
    BZOJ3834 : [Poi2014]Solar Panels
    BZOJ3831 : [Poi2014]Little Bird
    BZOJ3829 : [Poi2014]FarmCraft
    BZOJ2757 : [SCOI2012]Blinker的仰慕者
    BZOJ2707 : [SDOI2012]走迷宫
    给iOS工程增加Daily Build
    给NSString增加Java风格的方法
    象写程序一样写博客:搭建基于github的博客
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11238507.html
Copyright © 2011-2022 走看看