D. Palindrome Degree
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

String s of length n is called k-palindrome, if it is a palindrome itself, and its prefix and suffix of length  are(k - 1)-palindromes. By definition, any string (even empty) is 0-palindrome.

Let's call the palindrome degree of string s such a maximum number k, for which s is k-palindrome. For example, "abaaba" has degree equals to 3.

You are given a string. Your task is to find the sum of the palindrome degrees of all its prefixes.

Input

The first line of the input data contains a non-empty string, consisting of Latin letters and digits. The length of the string does not exceed 5·106. The string is case-sensitive.

Output

Output the only number — the sum of the polindrome degrees of all the string's prefixes.

Sample test(s)
input
a2A

output
1

input
abacaba

output
6

题意:

定义回文串的度为length 即前半部分串的度或后半部分的度+1。先再给你一个字符串。

要你求出他全部前缀度的和。

思路:

先用manacher求出以每一个位置为中心最大回文串的长度。

dp[i]记录长度为i的前缀的度。那么dp[i+1]分奇偶用到前面的度即可了。

具体见代码:

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=5110000;
int p[maxn<<1],dp[maxn],len;
char buf[maxn],st[maxn<<1];
void init()
{int i;len=strlen(buf);st[0]='$',st[1]='#';for(i=0;i<len;i++)st[2*i+2]=buf[i],st[2*i+3]='#';len=2*len+2;
}
void manacher()
{int i,id,mx=0;for(i=1;i<len;i++){p[i]=mx>i?

min(mx-i,p[2*id-i]):1; while(st[i+p[i]]==st[i-p[i]]) p[i]++; if(i+p[i]>mx) mx=i+p[i],id=i; } } int main() { int i,ans,n; while(~scanf("%s",buf)) { ans=0,n=strlen(buf); init(); manacher(); ans=dp[0]=1; for(i=1;i<n;i++) { if(p[i+2]-1>=i+1)//推断该前缀是否回文。字符串从0開始。

i+2为0到i在p数组对称中心无论回文串长度使是奇数还是偶数的。p[i]-1为最大回文长度 { if(i&1) dp[i]=dp[i/2]+1; else dp[i]=dp[i/2-1]+1; } ans+=dp[i]; } printf("%d\n",ans); } return 0; }

Hash的做法就比較简单了。

具体见代码:

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=5110000;;
unsigned long long H1[maxn],H2[maxn],xp[maxn],ha,hb,x=123;
int dp[maxn],len;
char buf[maxn],rbuf[maxn];
void init()
{int i;len=strlen(buf);H1[len]=H2[len]=0;xp[0]=1;for(i=len-1;i>=0;i--){H1[i]=H1[i+1]*x+buf[i];H2[i]=H2[i+1]*x+rbuf[i];xp[len-i]=xp[len-i-1]*x;}
}
unsigned long long getHash(unsigned long long *HS,int s,int L)
{return HS[s]-HS[s+L]*xp[L];
}
int main()
{int i,ans;while(~scanf("%s",buf)){ans=0,len=strlen(buf);for(i=0;i<len;i++)rbuf[i]=buf[len-i-1];init();ans=dp[0]=1;for(i=1;i<len;i++){ha=getHash(H1,0,i+1);hb=getHash(H2,len-i-1,i+1);if(ha==hb){if(i&1)dp[i]=dp[i/2]+1;elsedp[i]=dp[i/2-1]+1;}elsedp[i]=0;ans+=dp[i];}printf("%d\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/mfmdaoyou/p/6898857.html

codeforces7D Palindrome Degree(manacheramp;dp或Hshamp;dp)相关推荐

  1. codeforces7D Palindrome Degree(manacherdp或Hshdp)

    D. Palindrome Degree time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. poj3280 Cheapest Palindrome(回文串区间dp)

    https://vjudge.net/problem/POJ-3280 猛刷简单dp第一天第三题. 这个据说是[求字符串通过增减操作变成回文串的最小改动次数]的变体. 首先增减操作的实质是一样的,所以 ...

  3. 树形dp ---- gym101667 A(贪心 + 树形dp + 两个dp方程组维护)

    题目链接 题目大意: 就是一棵5e35e35e3的树,可以选择一些点,放上基站,如果uuu上的基站价值为ddd,那么距离uuu小于等于ddd的点都会被覆盖,问使得整棵树被覆盖需要的最小价值. 解题思路 ...

  4. leetcode10 为什么p[j-1] == '*'的时候,不能用递推公式dp[i][j] = dp[i][j-1] || dp[i][j-2] || dp[i-1][j]

    因为可能会出现以下情况: "mississippi" "mis*is*p*." mississ mis*is* 符合 mississi mis*is* 符合 所 ...

  5. codeforces D. Palindrome Degree(hash)

    开始使用Palindromic Characteristics的方式来计算dp(i,j)的回文度,然后统计dp所有(0,j)提示空间超过限制.因为是需要计算所有前缀的回文度之和.由于回文度关系有dp( ...

  6. 【BZOJ】1076 [SCOI2008]奖励关 期望DP+状压DP

    [题意]n种宝物,k关游戏,每关游戏给出一种宝物,可捡可不捡.每种宝物有一个价值(有负数).每个宝物有前提宝物列表,必须在前面的关卡取得列表宝物才能捡起这个宝物,求期望收益.k<=100,n&l ...

  7. 牛客网dp专题 数位dp

    文章目录 数位dp 例题: NC116652 uva11038 How many 0's NC15035 送分了QAQ NC20669 诡异数字 NC20665 7的意志 NC17385 Beauti ...

  8. [SOCI2005]最大子矩阵(DP) + [JXOI2018]守卫(DP) + [CQOI2016]手机号码(数位DP)[各种DP专练]

    DP专练博客 DP专练 T1:最大子矩阵 题目 题解 代码实现 T2:守卫 题目 题解 代码实现 T3:手机号码 题目 题解 代码实现 T1:最大子矩阵 题目 这里有一个n*m的矩阵,请你选出其中k个 ...

  9. 【HDU - 6662】Acesrc and Travel(树形dp,博弈dp)

    题干: Acesrc is a famous tourist at Nanjing University second to none. During this summer holiday, he, ...

最新文章

  1. java 外来类,外来的Matplotlib图(tkinter中的情节)
  2. 「BZOJ 2342」「SHOI 2011」双倍回文「Manacher」
  3. 湘潭计算机职业技术学校专业介绍,湘潭计算机职业技术学校介绍
  4. 读取一个product全部数据的工具
  5. 嵌入式开发中,用C++真香!
  6. firefox-Developer开发者站点——关于Object.create()新方法的介绍
  7. OC-@dynamic 关键字
  8. 第5篇K8S创建资源的两种方式
  9. mysql基本架构_MySQL的基本架构
  10. 贪心 or 动态规划 求解“最大字段和”问题(洛谷P1115题题解,Java语言描述)
  11. Matlab 数字滤波器设计大报告(数字信号处理课程设计)附代码
  12. 怎样增加phpmyadmin导入文件上限
  13. android ndk makefile,用Android NDK打造自己的toolchain(使用C/C++默认的Makefile)
  14. 阿尔派X09电脑调音软件 DSP调音软件下载
  15. python代码写龙卷风_python-打开网站时龙卷风403获取警告
  16. Python中IO编程-StringIO和BytesIO
  17. MIPI接口和DVP接口摄像头学习笔记
  18. Matlab多行注释单行程序分行写
  19. Ubuntu 16.04 单显卡安装Nvidia驱动+GTX750显卡安装CUDA 9.1+cuDNN 7.1.3
  20. 【51单片机快速入门指南】5.2:SPI读取 12位ADC XPT2046 芯片

热门文章

  1. Acer宏基笔记本Aspire F 15 F5-573G-50SZ拆机清灰教程
  2. 06 系统建模语言SysML——内部模块图
  3. 登陆梦幻显示服务器列表错误,常见问题——17173.com网络游戏:《梦幻西游》专区...
  4. PLM是什么?-数字化转型网
  5. 2022 年值得推荐的 React 库
  6. 【Python入门】day-5 format
  7. 《炬丰科技-半导体工艺》微泡基础知识及其在半导体清洗中的应用
  8. Java趣味问答题^_^
  9. 正则表达式|爬取百度图片
  10. (还需要阅读代码)FoldingNet论文阅读