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;
else
dp[i]=dp[i/2-1]+1;
}
else
dp[i]=0;
ans+=dp[i];
}
printf("%d\n",ans);
}
return 0;
}

codeforces7D Palindrome Degree(manacher&amp;dp或Hsh&amp;dp)的更多相关文章

  1. Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp

    题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...

  2. Codeforces Beta Round #7 D. Palindrome Degree hash

    D. Palindrome Degree 题目连接: http://www.codeforces.com/contest/7/problem/D Description String s of len ...

  3. Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  4. Codeforces Beta Round #7 D. Palindrome Degree —— 字符串哈希

    题目链接:http://codeforces.com/contest/7/problem/D D. Palindrome Degree time limit per test 1 second mem ...

  5. Misha and Palindrome Degree

    Misha and Palindrome Degree 题目链接:http://codeforces.com/problemset/problem/501/E 贪心 如果区间[L,R]满足条件,那么区 ...

  6. Palindrome Degree(hash的思想题)

    个人心得:这题就是要确定是否为回文串,朴素算法会超时,所以想到用哈希,哈希从左到右和从右到左的key值一样就一定是回文串, 那么问题来了,正向还能保证一遍遍历,逆向呢,卡住我了,后面发现网上大神的秦九 ...

  7. HDU 4632 Palindrome subsequence (2013多校4 1001 DP)

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  8. Palindrome Degree(CodeForces 7D)—— hash求回文

    学了kmp之后又学了hash来搞字符串.这东西很巧妙,且听娓娓道来. 这题的题意是:一个字符串如果是回文的,那么k值加1,如果前一半的串也是回文,k值再加1,以此类推,算出其k值.打个比方abaaba ...

  9. hoj 2662 经典状压dp // MyFirst 状压dp

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2662 1.引言:用dp解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态. ...

随机推荐

  1. 《Javascript权威指南》学习笔记之十五:BOM之源---window对象

    BOM是Browser Object Model的缩写,即浏览器对象模型,提供了独立于网页内容和浏览器窗体之间进行交互的APi.API由若干对象组成,因为浏览器是Javascript的宿主,因此,这些 ...

  2. HDU 3292

    快速幂模+佩尔方程 #include <iostream> #include <cstdio> #include <algorithm> #include < ...

  3. Mysql第四天 数据库设计

    不考虑主备.集群等方案,基于业务上的设计主要是表结构及表间关系的设计. 而关于表中字段主要是依据业务来进行定义,我们能够指定的大概有这么几项: 存储引擎 一般用InnoDB,特殊需求特殊选用 字符集和 ...

  4. android AppWidget的使用以及利用TimerTask实现widget的定时更新

    第一步:首先是Widget的定义声明: 在资源文件下的xml目录中建立文件example_appwidget_info.xml: <?xml version="1.0" en ...

  5. Android SQLiteDatabase分析

    Android中的数据存储使用的小巧的SQLite数据库. 为了方便java层使用SQLite,android做了大量的封装.提供了一些列的类和API.本文章就揭露这些封装背后的类图关系. 老规矩,首 ...

  6. 深入理解 C 指针阅读笔记 -- 第五章

    Chapter5.h #ifndef __CHAPTER_5_ #define __CHAPTER_5_ /*<深入理解C指针>学习笔记 -- 第五章*/ /*不应该改动的字符串就应该用 ...

  7. DB-MySQL:MySQL 临时表

    ylbtech-DB-MySQL:MySQL 临时表 1.返回顶部 1. MySQL 临时表 MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysq ...

  8. ROS-多机通信

    前言:一定要在同一路由的局域网下进行,就是两台电脑的ip要像这样:192.168.191.4和192.168.191.8,只有最后一位不同,这样就能ping通了,否则ping不同. 一.查看ip和主机 ...

  9. BZOJ 2821 分块+二分

    题意: N个数,M组询问,每次问[l,r]中有多少个数出现正偶数次. 思路: 把N个数分成sqrt(n)块,预处理d[i][j]表示第i块起点到第j块末尾的答案 枚举起点i,并维护一个数组记录每个数到 ...

  10. View的呈现(二)加载流程

    这块涉及到Code+Razor模板=>html[output流] 而这块的问题在于Razor最后生成了什么?--对象:一个类文件:eg:index.cshtml  => index_cst ...