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.

Examples
Input
a2A
Output
1
Input
abacaba
Output
6

(一道好题,我想到的是用manacher做,听说还可以用kmp和hash做

题意:遍历给定字符串的所有前缀,如果该前缀是个回文串,将该串对半分看还是不是个回文串,如果是,则其degree+1,如cc是个回文串,c也是个回文串,所以cc的degree==1

解题思路:跑一边manacher,用p数组判断前缀是不是回文串,如果是回文串,则它的degree是其对半分后串的degree+1。

ac代码:
 1 #include <cstdio>
2 #include <cstring>
3 #include <string>
4 #include <algorithm>
5 using namespace std;
6 typedef long long ll;
7 const int maxn = 5*1e6+10;
8 char st[maxn];
9 char s[maxn<<1];
10 int p[maxn<<1];
11 int ans[maxn];
12 void init(int le) {
13 for(int i=le-1;i>=0;--i) {
14 s[2*i+2]=st[i];
15 s[2*i+3]='*';
16 }
17 s[0]='$';
18 s[1]='*';
19 }
20 void manacher(int le) {
21 int mx=0,id=0;
22 for(int i=1;i<2*le+2;++i) {
23 p[i]=mx>i?min(p[id*2-i],mx-i):1;
24 while(s[i+p[i]]==s[i-p[i]]) p[i]++;
25 if(i+p[i]>mx) {
26 mx=i+p[i];
27 id=i;
28 }
29 }
30 }
31 int main() {
32 ll res=0;
33 scanf("%s",st);
34 int le = strlen(st);
35 init(le);
36 manacher(le);
37 int u=2;
38 for(int i=0;i<le;++i) {
39 if(p[i+2]!=i+2) {
40 ans[i]=0;
41 }
42 else {
43 ans[i]=ans[(i-1)/2]+1;
44 ans[i]=max(ans[i],1);
45 }
46 res+=ans[i];
47 }
48 printf("%lld\n",res);
49 }
50
51
52
53

codeforces 7D的更多相关文章

  1. CodeForces - 7D Palindrome Degree

    最近接触了一点字符串算法,其实也就是一个简单的最大回文串算法,给定字符串s,求出最大字符串长度. 算法是这样的, 用'#'将s字符串中的每个字符分隔,比如s = "aba",分割后 ...

  2. CodeForces 7D Palindrome Degree 字符串hash

    题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...

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

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

  4. Codeforces Beta Round #7--D. Palindrome Degree(Manacer)

    题目:http://blog.csdn.net/winddreams/article/details/44218961 求出每一个点为中心的最长字符串,推断该串是不是从开头的回文串. #include ...

  5. Codeforces Round #509 (Div. 2) F. Ray in the tube(思维)

    题目链接:http://codeforces.com/contest/1041/problem/F 题意:给出一根无限长的管子,在二维坐标上表示为y1 <= y <= y2,其中 y1 上 ...

  6. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  9. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

随机推荐

  1. ASP.NET MVC--sqlserver数据库脚本的导入导出

    1.右键选择数据库---任务----生成脚本 2.弹出如下框 导出整个表,默认下一步,否则选择特定数据库对象表单选框 3.修改文件名路径,可以保存脚本到制定路径,否则为默认,点击高级进入 要编写脚本的 ...

  2. 动态sql语句、逆向工程(generator)、分页助手(pagehelper)

    1.动态sql语句 if if where 配合使用 <select id="selectByWhere" resultType="com.alibaba.wlq. ...

  3. Databricks 第9篇:Spark SQL 基础(数据类型、NULL语义)

    Spark SQL 支持多种数据类型,并兼容Python.Scala等语言的数据类型. 一,Spark SQL支持的数据类型 整数系列: BYTE, TINYINT:表示1B的有符号整数 SHORT, ...

  4. uni-app开发经验分享七: 有关列表数据下拉加载方法的解析及记录

    在使用uni.request获取后台数据时,我们往往碰到一个问题,列表的懒加载及数据实时更新,这里记录下我制作这类功能的方法. 问题描述:后台返回数据,前端需要进行10个为一组来分页,先显示前10个, ...

  5. 使用 .NETCore自带框架快速实现依赖注入

    Startup 在Startup的ConfigureServices()中配置DI的接口与其实现 public void ConfigureServices(IServiceCollection se ...

  6. Obligations for calling close() on the iterable returned by a WSGI application

    Graham Dumpleton: Obligations for calling close() on the iterable returned by a WSGI application. ht ...

  7. try-catch 异常捕获学习

    #include <iostream> #include <string> #include <vector> #include <stdexcept> ...

  8. Java泛型机制

    泛型程序设计 1.泛型程序设计的起源? 泛型是JDK1.5增加的新特性. 2.使用泛型的好处? 使用泛型机制编写的代码比那些杂乱使用Object变量,然后再进行强制类型转换的代码具有更好的安全性和可读 ...

  9. 如何在opencv下使用SIFT

    SIFT即尺度不变特征变换,是用于图像处理领域的一种描述.这种描述具有尺度不变性,可在图像中检测出关键点,是一种局部特征描述子.SIFT的尺度不变特征变换在图像特征点匹配中十分关键,因为我们从不同角度 ...

  10. python 文件的方法

    1.open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError. 注意:使用 open ...