分割实际上就是不断地从两端取出一样的一段,并对剩下的串进行分割。下面我们来证明一下每一次贪心取出最短一段的正确性:

考虑两种分割方式,分别表示成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的更多相关文章

  1. PALIN - The Next Palindrome 对称的数

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

  2. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  3. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  4. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  5. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  6. [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 ...

  7. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  8. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  9. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

随机推荐

  1. 学习使用SignalR

    1.创建空白的控制台程序 2.添加两个NuGet包(Microsoft.AspNet.SignalR.SelfHost.Microsoft.Owin.Cors.Topshelf)Topshelf用于快 ...

  2. Java领域的表现层的三种技术--jsp、freemarker、velocity

    10月份忙碌的参加秋招并获得了5个成都.上海.广州等不同地区的工作offer,最终选择了广州,11月底来到公司实习,很快,就跟进了公司的项目了,原本以为可以和宣讲会时报的志愿一样--Java开发,但是 ...

  3. Winform同步调用异步函数死锁原因分析、为什么要用异步

    1.前言 几年前,一个开发同学遇到同步调用异步函数出现死锁问题,导致UI界面假死.我解释了一堆,关于状态机.线程池.WindowsFormsSynchronizationContext.Post.co ...

  4. Ajax样例

    $.ajax({ url : "newsservlet",//请求地址 dataType : "json",//数据格式 type : "post&q ...

  5. Arthas 进阶教程

    Arthas 进阶教程 启动math-game 下载demo-arthas-spring-boot.jar,再用java -jar命令启动: wget https://github.com/hengy ...

  6. Endian

    Endian 寻址 多字节对象被存储为连续的字节序列,对象的地址为所使用字节中最小的地址. 例如,假设一个类型为 int 的变量 a 的地址为 0x100,也就是说,地址表达式 &a 的值为 ...

  7. TStor-OneCOS ,主打专一海量对象场景

    ​谁能与你厮守终身 生活在21世纪,最常见的事莫过于更新换代了,找一款能长久适用的产品,是很多人都希望拥有的,特别是针对于云服务产品,而对象·混合云存储新增 TStor-OneCOS 就是这样一款可以 ...

  8. python flask1

    以这个服务端代码为例,简单了解一下flask的运用. 1.app = Flask(__name__)记住就好了 2.@app.route("/")记住就好了:注意括号里的是调用这个 ...

  9. oo第一单元学习总结

    写在开头: 第一次接触面向对象思想和java语言,在学习以及完成作业的过程经历了一个比较痛苦的过程, 虽然在每次写作业时总是会有一些小小的抱怨,虽然写出的代码还是很差, 但是看到自己有所进步,还是感觉 ...

  10. str数组