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

考虑两种分割方式,分别表示成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. Linux基础安全配置(centos7)

    1.帐户口令的生存期不长于90天 sed -i.old 's#99999#90#g' /etc/login.defs egrep "90" /etc/login.defs 2.密码 ...

  2. Java基础之(二):Notepad++实现HelloWorld

    现在我们开始编写我们的第一个程序:Hello World! HelloWorld 新建一个java文件 文件后缀名为.java Hello.java 代码分析: 接下来写完最大的框之后,那接下来当然就 ...

  3. Java(10)认识类和对象

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201574.html 博客主页:https://www.cnblogs.com/testero ...

  4. 内网渗透DC-3靶场通关

    个人博客:点我 DC系列共9个靶场,本次来试玩一下DC-3,只有1个flag,下载地址. 下载下来后是 .ova 格式,建议使用vitualbox进行搭建,vmware可能存在兼容性问题.靶场推荐使用 ...

  5. 更好的 java 重试框架 sisyphus 入门简介

    What is Sisyphus sisyphus 综合了 spring-retry 和 gauva-retrying 的优势,使用起来也非常灵活. 为什么选择这个名字 我觉得重试做的事情和西西弗斯很 ...

  6. css3鼠标悬停图片边框线条动画特效

    css3鼠标经过内容区时,边框线条特效效果制作.   html: <div class="strength grWidth hidden"> <div class ...

  7. LeetCode:动态规划

    动态规划 动态规划永远的神 这部分主要是学习了 labuladong 公众号中对于动态规划的讲解 刷了些 leetcode 题,在此做一些记录,不然没几天就忘光光了 题目 这部分内容直接上题目了,解题 ...

  8. redis中lua脚本的简单使用

    一.背景 在使用redis的过程中,发现有些时候需要原子性去操作redis命令,而redis的lua脚本正好可以实现这一功能.比如: 扣减库存操作.限流操作等等. redis的pipelining虽然 ...

  9. Shadertoy 教程 Part 2 - 圆和动画

    Note: This series blog was translated from Nathan Vaughn's Shaders Language Tutorial and has been au ...

  10. 数组中只出现过一次的数字 牛客网 剑指Offer

    数组中只出现过一次的数字 牛客网 剑指Offer 题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了偶数次.请写程序找出这两个只出现一次的数字. def FindNumsAppearOnce ...