[noi34]palindrome
分割实际上就是不断地从两端取出一样的一段,并对剩下的串进行分割。下面我们来证明一下每一次贪心取出最短一段的正确性:
考虑两种分割方式,分别表示成S=A+B+A和S=C+D+C,其中A就是最短的一段,那么当2|A|<|C|,显然就有C=A+E+A,那么就可以用三次划分来完成C,而当|A|<|C|<2|A|,即A在C中重叠了,那么最短的前缀一定不是A,而是重叠部分(重叠部分既是A的前缀又是A的后缀)。
那么这个贪心就成立了,用hash暴力判断即可。

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define mod 1000000007
4 #define ll long long
5 #define N 1000001
6 int t,l;
7 ll sum[N],mi[N];
8 char s[N];
9 ll calc(int l,int r){
10 return ((sum[r]-sum[l-1]*mi[r-l+1])%mod+mod)%mod;
11 }
12 int work(int l,int r){
13 if (l>r)return 0;
14 int mid=(l+r+1>>1),k=l;
15 while ((k<mid)&&(calc(l,k)!=calc(l+r-k,r)))k++;
16 if (k==mid)return 1;
17 return work(k+1,l+r-k-1)+2;
18 }
19 int main(){
20 mi[0]=1;
21 for(int i=1;i<N;i++)mi[i]=(mi[i-1]*29)%mod;
22 scanf("%d",&t);
23 while (t--){
24 scanf("%s",s);
25 l=strlen(s);
26 sum[0]=s[0]-'a'+1;
27 for(int i=1;i<l;i++)sum[i]=(sum[i-1]*29+s[i]-'a'+1)%mod;
28 printf("%d\n",work(0,l-1));
29 }
30 }
[noi34]palindrome的更多相关文章
- PALIN - The Next Palindrome 对称的数
A positive integer is called a palindrome if its representation in the decimal system is the same wh ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- Conversion Tools(转换工具)
转换工具 1.Excel # Process: Excel 转表 arcpy.ExcelToTable_conversion("", 输出表, "") # Pr ...
- 题解 「BZOJ2137」submultiple
题目传送门 题目大意 给出 \(M,k\) ,求出 \[\sum_{x|M}\sigma(x)^k \] 给出 \(P_i\),满足 \(n=\prod_{i=1}^{n}a_i^{P_i}\),其中 ...
- 洛谷1501 Tree II(LCT,路径修改,路经询问)
这个题是一个经典的维护路径信息的题,对于路径上的修改,我们只需要把对应的链\(split\)上来,然后修改最上面的点就好,注意pushdown的时候的顺序是先乘后加 然后下传乘法标记的时候,记得把对应 ...
- 新一代数据科学ide平台DataSpell提前发行版体验
1 简介 PyCharm开发公司jetbrains专门面向数据科学的ide项目DataSpell在前不久发布了其EAP版本(早期预览版本),为我们带来了诸多趋于成熟的功能特性,本文就将为大家介绍其使用 ...
- javascript-原生-面向对象
1.javascript面向对象程序设计 概述:javascript不想其他面向对象编程语言那样有类的概念,javascript没有类(构造函数)的概念,只有对象的概念. 2.理解javascript ...
- noj加1乘2平方
广度优先搜索典例 00 题目 描述: 最简单的队列的使用#include <iostream>#include <queue>using namespace std;queue ...
- 【UE4】类的继承层级关系
- 一文看懂JVM内存区域分布与作用
那么我们在开始介绍Java内存区域之前,我们先放一张内存区域的图,方便我们后面介绍的时候可以对照着看. 须知,本文是根据JDK8来介绍的. 程序计数器 首先它是线程私有的,它也称为代码的行号指示器,字 ...
- cf 11B Jumping Jack(贪心,数学证明一下,,)
题意: 给一个数X. 起始点为坐标0.第1步跳1格,第2步跳2格,第3步跳3格,.....以此类推. 每次可以向左跳或向右跳. 问最少跳几步可以到坐标X. 思路: 假设X是正数. 最快逼近X的方法是不 ...
- 经典200例-002 为项目添加DLL文件引用
项目右击,添加引用,(或菜单栏选择"项目","添加引用"),COM选项卡 复制去Google翻译翻译结果