codeforces 7D
1 second
256 megabytes
standard input
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.
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
(一道好题,我想到的是用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的更多相关文章
- CodeForces - 7D Palindrome Degree
最近接触了一点字符串算法,其实也就是一个简单的最大回文串算法,给定字符串s,求出最大字符串长度. 算法是这样的, 用'#'将s字符串中的每个字符分隔,比如s = "aba",分割后 ...
- CodeForces 7D Palindrome Degree 字符串hash
题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...
- Palindrome Degree(CodeForces 7D)—— hash求回文
学了kmp之后又学了hash来搞字符串.这东西很巧妙,且听娓娓道来. 这题的题意是:一个字符串如果是回文的,那么k值加1,如果前一半的串也是回文,k值再加1,以此类推,算出其k值.打个比方abaaba ...
- Codeforces Beta Round #7--D. Palindrome Degree(Manacer)
题目:http://blog.csdn.net/winddreams/article/details/44218961 求出每一个点为中心的最长字符串,推断该串是不是从开头的回文串. #include ...
- Codeforces Round #509 (Div. 2) F. Ray in the tube(思维)
题目链接:http://codeforces.com/contest/1041/problem/F 题意:给出一根无限长的管子,在二维坐标上表示为y1 <= y <= y2,其中 y1 上 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
随机推荐
- 如何创建一个 PostgreSQL 数据库?
PostgreSQL 官网截图 PostgreSQL 是什么? PostgreSQL 是一个功能非常强大的,历史悠久,开源的关系数据库.PostgreSQL支持大部分的SQL标准并且提供了很多其他现代 ...
- vue-cli3x4x修改本地端口port
一.推荐方法 "scripts": { "serve": "vue-cli-service serve --port 3000", &quo ...
- python 编译EXE文件
以labelme测试 标注工具labelimg和labelme 矩形标注工具:labelimg 多边形标准工具:labelme 前者官网发布了可执行文件,后者只有python源码,如果需要编译wind ...
- 提取当前文件夹下的所有文件名.bat(Windows批处理文件)
@echo off dir /s/b *.* > 文件名.txt exit
- C++ 无法打开 源 文件 "ntddk.h"
原因是SDK版本太高了,或者版本不对应WDK,换一个SDK版本就好了.
- Azure DevOps Pipelines执行RobotFramework自动化代码
一.Azure DevOps介绍 1.什么是 Azure DevOps? Azure DevOps其实是VSTS(Visual Studio Team Service)更名后的名字.而VSTS是TFS ...
- 理解和运用 ClassLoader 该篇文章就够了
定义 根据<深入理解Java虚拟机>提到"通过一个类的全限定名(packageName.ClassName)来获取描述此类的二进制字节(class文件字节)这个动作的代码模块就叫 ...
- 强连通分量 与 2-SAT
近期一直在刷这方面的题 因为没法学新知识 但又想写点什么 就水篇博文吧 引理 简单来说,在一个有向图中,若所有点之间两两互相直接可达,则将这个图成为强连通分量 强连通分量可以是某个有向图中的子图 求强 ...
- Fastjson使用实例
Fastjson使用实例 一.FastJson使用范例 1.1FastJson三个核心类 1.2Maven依赖 1.3Scala API 1.3.1反序列化 1.3.2序列化 1.4Java API ...
- socket套接字编程(1)——基本函数
TCP交互流程: 服务器:1. 创建socket:2. 绑定socket和端口号:3. 监听端口号:4. 接收来自客户端的连接请求:5. 从socket中读取字符:6. 关闭socket. 客户端:1 ...