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 ...
随机推荐
- 【源码解读】js原生消息提示插件
效果如下: 关闭message后前后message的衔接非常丝滑,这部分是我比较感兴趣的.带着这个问题先了解下DOM结构,顺便整理下作者的思路. 从DOM里我们可以看到所有的message都在一个容器 ...
- MySQL ---- 锁知识
锁 我们知道mysql中支持很多个存储引擎,在不同的存储引擎下所能支持的锁是不同的,我们通过MyISAM和InnoDB来进行一下对比. 表级锁定(table-level) 表级别的锁定是MySQL ...
- [阿里DIEN] 深度兴趣进化网络源码分析 之 Keras版本
[阿里DIEN] 深度兴趣进化网络源码分析 之 Keras版本 目录 [阿里DIEN] 深度兴趣进化网络源码分析 之 Keras版本 0x00 摘要 0x01 背景 1.1 代码进化 1.2 Deep ...
- JavaScript中创建数组的方式!
JavaScript中创建数组的方式! 利用数组字面量 // 1 直接量 console.log(Array.prototype); var arr = [1, 2, 4, 87432]; // 注意 ...
- Java层面上下文切换
前言 在过去单CPU时代,单任务在一个时间点只能执行单一程序.之后发展到多任务阶段,计算机能在同一时间点并行执行多任务或多进程.虽然并不是真正意义上的"同一时间点",而是 多个任务 ...
- Go 如何实现热重启
https://mp.weixin.qq.com/s/UVZKFmv8p4ghm8ICdz85wQ Go 如何实现热重启 原创 zhijiezhang 腾讯技术工程 2020-09-09
- gstack pstack strace
gstack pstack strace 通过进程号 查看 进程的工作目录 Linux神器strace的使用方法及实践 - 知乎 https://zhuanlan.zhihu.com/p/180053 ...
- Android使用代码开关Location服务
Android系统中,只有系统设置里面有入口开关位置服务.其他的应用应该怎么去开关这个服务呢? 首先,应用需要有系统权限(签名),在这基础上,我们就可以通过一些手段来实现这个功能. 这里要注意一点,不 ...
- 使用Robo 3T操作MongoDB数据库
安装Robo 3T连接MongoDB数据库教程:https://blog.csdn.net/baidu_39298625/article/details/98845789 在IDEA中用三个jar包链 ...
- LOJ10077
题目描述给出一个 N 个顶点 M 条边的无向无权图,顶点编号为 1∼N.问从顶点 1 开始,到其他每个点的最短路有几条. 输入格式第一行包含 2 个正整数 N,M,为图的顶点数与边数. 接下来 M行, ...