Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp
题目链接:
http://codeforces.com/problemset/problem/7/D
D. Palindrome Degree
time limit per test1 secondmemory limit per test256 megabytes
#### 问题描述
> 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.
#### 输入
> 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 the only number — the sum of the polindrome degrees of all the string's prefixes.
样例输入
a2A
样例输出
1
样例输入
abacaba
样例输出
6
题意
假设前缀prei是k-回文串(k=a[i])。则求ans=sigma(a[i])。
题解
用manacher算法处理出最左端为第一个字母的所有回文串,然后dp一下。
dp[i]表示长度为i的前缀是k-回文串(k=dp[i])。 则dp[i]=dp[i/2]+1;
ans=sigma(dp[i]);
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1e7+10;
char s[maxn];
//P[i]表示把回文串折叠起来的长度
//mx表示当前计算出来的回文串往右延伸的最远端
//d表示贡献出mx的串的回文中心
int P[maxn],mx,id,n;
LL dp[maxn];
LL solve(){
LL ret=0;
int len=strlen(s+1);
n=len*2+1;
s[0]='$',s[n]='#',s[n+1]='\0';
for(int i=len*2;i>=1;i--){
if(i&1) s[i]='#';
else s[i]=s[i/2];
}
// puts(s);
clr(dp,0);
mx=1,id=0;
for(int i=1;i<=n;i++){
//优化的核心,画画图比较好理解,j=2*id-i表示i关于id对称的点
P[i]=mx>i?min(mx-i,P[2*id-i]):1;
int k=i+P[i];
while(s[k]==s[2*i-k]) k++,P[i]++;
if(2*i-k==0&&i>1){
// bug(i);
int l=P[i]-1;
dp[l]=dp[l/2]+1;
ret+=dp[l];
}
if(k>mx){
mx=k;
id=i;
}
}
return ret;
}
int main() {
scf("%s",s+1);
LL ans=solve();
prf("%I64d\n",ans);
return 0;
}
//end-----------------------------------------------------------------------
Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp的更多相关文章
- Codeforces Beta Round #7 D. Palindrome Degree hash
D. Palindrome Degree 题目连接: http://www.codeforces.com/contest/7/problem/D Description String s of len ...
- Codeforces Beta Round #7 D. Palindrome Degree —— 字符串哈希
题目链接:http://codeforces.com/contest/7/problem/D D. Palindrome Degree time limit per test 1 second mem ...
- Codeforces Beta Round #51 D. Beautiful numbers(数位dp)
题目链接:https://codeforces.com/contest/55/problem/D 题目大意:给你一段区间[l,r],要求这段区间中可以整除自己每一位(除0意外)上的数字的整数个数,例如 ...
- Codeforces Beta Round #96 (Div. 2) E. Logo Turtle dp
http://codeforces.com/contest/133/problem/E 题目就是给定一段序列,要求那个乌龟要走完整段序列,其中T就是掉头,F就是向前一步,然后开始在原点,起始方向随意, ...
- Codeforces Beta Round #96 (Div. 1) C. Logo Turtle —— DP
题目链接:http://codeforces.com/contest/132/problem/C C. Logo Turtle time limit per test 2 seconds memory ...
- Codeforces Beta Round #17 C. Balance (字符串计数 dp)
C. Balance time limit per test 3 seconds memory limit per test 128 megabytes input standard input ou ...
- Codeforces Beta Round #96 (Div. 1) C. Logo Turtle DP
C. Logo Turtle A lot of people associate Logo programming language with turtle graphics. In this c ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #62 题解【ABCD】
Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...
随机推荐
- 4. HTML表单标签
表单是网页中最常见的元素,也是用户和我们交互的重要手段,在网站中的登录.注册.信息更新这些功能都是依赖表单实现的.在HTML中对于表单提供了一系列的标签,即输入框.下拉框.按钮.文本域,如下是一个最常 ...
- Delphi调用爷爷类的方法
Delphi通过inherited 可以调用父类的方法,但是没有提供直接调用父类的父类的方法(爷爷类),通过变通的方式实现如下: 假设父类是TFather,爷爷类TGrand,调用爷爷类的Write方 ...
- mysql5.6升级为mysql5.7部署jboss/wildfly应用项目
一.部署mysql5.7二进制版 解压tar -xvf mv mysql-5.7 /usr/local/mysql5.7 或者其他文件夹 cd /usr/local/mysql.57 usera ...
- BurpSuite—-Repeater模块(中继器)
一.简介 Burp Repeater 是一个手动修改并补发个别 HTTP 请求,并分析他们的响应的工具.它最大的用途就是和其他 Burp Suite 工具结合起来.你可以从目标站点地图,从 Burp ...
- SVN-Tips
一些实际使用中遇到与学习的SVN的TIPS 1.如何从SVN检出maven项目: 1.从svn导入“作为工作空间中的项目检出”--->Finish 2.如“1”操作后,会自动的在MyEclips ...
- Hadoop命令大全
Hadoop命令大全 分类: 云计算2011-03-01 15:04 6852人阅读 评论(0) 收藏 举报 hadoop作业任务集群class脚本 1.列出所有Hadoop Shell支持的命令 ...
- 20155325 加分作业 实现pwd
要求 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd 准备 思路 问题 1.如何获取当前目录的节点号 Linux ...
- 20145209刘一阳《JAVA程序设计》第一周课堂测试
第一周课堂测试 1.下列不属于Java后继技术的是(D) A .Android B .JSP C .XML D .Python 2.下列关于Java语言特点的描述,正确的一组是(C) A .面向过程: ...
- html 第一行不缩进,第二行缩进,文字对齐
<p style="text-indent: -7rem;margin-left: 7rem;"> <img src="images/记录要求.png& ...
- Mac 用Ctr+C复制,Ctr+V 粘贴
用习惯Windows的用户,进入Mac,不习惯快捷方式. 用下面的方法,可以返回windows 习惯. 1.进入系统偏好设置->键盘->修饰键 2.Control 选择 Command,C ...