zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #7--D. Palindrome Degree(Manacer)

    题目:http://blog.csdn.net/winddreams/article/details/44218961

    求出每一个点为中心的最长字符串,推断该串是不是从开头的回文串。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std ;
    int p[12000000] , dp[6000000];
    char s[12000000] , str[12000000] ;
    int init() {
        int i , j , l ;
        l = strlen(s) ;
        str[0] = '&' ;
        for(i = 0 , j = 1 ; i < l ; i++) {
            str[j++] = '#' ;
            str[j++] = s[i] ;
        }
        str[j++] = '#' ;
        str[j] = '' ;
        return j ;
    }
    void Manacer(int l) {
        int i , max1 = 0 , id ;
        p[0] = 0 ;
        for(i = 1 ; i < l ; i++) {
            if( max1 > i )
                p[i] = min(p[2*id-i],max1-i) ;
            else
                p[i] = 1 ;
            while( str[ i-p[i] ] == str[ i+p[i] ] )
                p[i]++ ;
            if( p[i]+i > max1 ) {
                max1 = p[i] + i ;
                id = i ;
            }
            //printf("%d ", p[i]) ;
        }
        //printf("
    ") ;
    }
    int main() {
        int i , j , l , x , ans = 0 ;
        scanf("%s", s) ;
        l = init() ;
        Manacer(l) ;
        memset(dp,0,sizeof(dp)) ;
        for(i = 2 ; i < l/2+1 ; i++) {
            if( i%2 == 1 && i/2 == (p[i]-1)/2 ) {
                x = p[i] - 1 ;
                dp[x] = dp[x/2] + 1 ;
                ans += dp[x] ;
            }
            else if( i%2 == 0 && p[i] == i ) {
                x = p[i] - 1 ;
                dp[x] = dp[x/2] + 1 ;
                ans += dp[x] ;
            }
        }
        printf("%d
    ", ans) ;
    }
    


     

  • 相关阅读:
    ASP.NET 5
    asp.net web api
    PowerShell 常用命令
    WinDbg
    Visual Studio Online
    asp.net authentication
    CoreOS
    解决download.msdn.microsoft.com无法正确解析而无法下载的问题
    nodejs express
    移动平台WEB前端开发技巧汇总
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4506708.html
Copyright © 2011-2022 走看看