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] = '\0' ;
    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("\n") ;
}
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\n", ans) ;
}
时间: 2024-11-19 05:32:13

Codeforces Beta Round #7--D. Palindrome Degree(Manacer)的相关文章

Codeforces Global Round 7【ABCD】(题解)

目录 涵盖知识点:思维.构造.马拉车. 比赛链接:传送门 D题只有数据范围的区别,故只写D2. 好多题啊,随缘更新.(其实懒得写) A - Bad Ugly Numbers B - Maximums C - Permutation Partitions D2 - Prefix-Suffix Palindrome (Hard version) 涵盖知识点:思维.构造.马拉车. 比赛链接:传送门 D题只有数据范围的区别,故只写D2. 好多题啊,随缘更新.(其实懒得写) A - Bad Ugly Nu

Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)

C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform it

Codeforces Beta Round #12 D. Ball (线段树)

题目大意: n个女性中,如果有一个女性的三维比这个女性的三维都大,这个女性就要自杀.问要自杀多少个. 思路分析: 先按照第一维排序. 然后离散化第二维,用第二维的下标建树,树上的值是第三维,更新的时候取最大值. 注意是按照第一维度从大到小进入线段树. 而且还要严格递增. 所以处理第一维度比较大小的时候要分开处理,要把相同的先判断,再更新入树. 那么如何判断这个女性是否自杀. 首先,你知道第一维度是从大到小的,所以先进树了的节点的第一维度一定更大. 再次,我们考虑第二维度,我们去树上第二维度下标大

Codeforces Beta Round #6 (Div. 2 Only)

Codeforces Beta Round #6 (Div. 2 Only) A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 typedef long long ll; 8 /*#ifndef

Codeforces Beta Round #7

Codeforces Beta Round #7 http://codeforces.com/contest/7 A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 typedef long lon

Codeforces Beta Round #14 (Div. 2)

Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 A 找最大最小的行列值即可 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 500005

Codeforces Beta Round 84 (Div. 2 Only)

layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 不得不说这远古场太简单了 A - Nearly Lucky Number (签到) 题意 给出一个数字 求其中7和4的数目是否是7和4组成 #include<bits/stdc++.h> using namespa

Codeforces Beta Round #73 (Div. 2 Only)

Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7

Codeforces Beta Round 77 (Div. 2 Only)

layout: post title: Codeforces Beta Round 77 (Div. 2 Only) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 A - Football (签到) 题意 问有没有连续的七个1或者七个0 #include<bits/stdc++.h> using namespace std; typedef long long ll; typ