[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 ...
随机推荐
- 阿里 Midway 正式发布 Serverless v1.0,研发提效 50%
Github:https://github.com/midwayjs/midway 开源为了前端和 Node.js 的发展,点 Star! 去年阿里提出 Serverless 架构,并利用其新一代研发 ...
- 《看漫画学Pyhton》中计算水仙花数
利用while循环实现 i = 100 r = 0 s = 0 t = 0 while i < 1000: r = i // 100 s = (i - r * 100) // 10 t = i ...
- Head First Python 代码和实例下载
http://python.itcarlow.ie/resources.html
- 【UE4 C++ 基础知识】<14> 多线程——AsyncTask
概念 AsyncTask AsyncTask 系统是一套基于线程池的异步任务处理系统.每创建一个AsyncTas,都会被加入到线程池中进行执行 AsyncTask 泛指 FAsyncTask 和 FA ...
- OO_JAVA_电梯运行模拟_单元总结
电梯运行模拟--三次作业总结 目录 电梯运行模拟--三次作业总结 总体遵循的设计思路 逻辑解耦 电梯与调度器解耦 楼层信息的存储和变更与电梯.调度器解耦 调度器运行流程解耦 第一次电梯,蠢笨串行先到先 ...
- 2021.9.22考试总结[NOIP模拟59]
T1 柱状图 关于每个点可以作出两条斜率绝对值为\(1\)的直线. 将绝对值拆开,对在\(i\)左边的点\(j\),\(h_i-i=h_j-j\),右边则是把减号换成加号. 把每个点位置为横坐标,高度 ...
- [BZOJ3307] 雨天的尾巴-----------------线段树进阶
虽然是个板子,但用到了差分思想. Description N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最 ...
- linux ar
转载:Linux ar命令 | 菜鸟教程 (runoob.com) Linux ar命令用于建立或修改备存文件,或是从备存文件中抽取文件. ar可让您集合许多文件,成为单一的备存文件.在备存文件中,所 ...
- OpenWrt编译报错:Package airfly_receiver is missing dependencies for the following libraries
今天在编译一个OpenWrt测试用例的时候出现报错 Package airfly_receiver is missing dependencies for the following librarie ...
- AXI总线简介、ID分析、DMA、Vivado烧录、系统集成
转载:https://blog.csdn.net/CrazyUncle/article/details/89918030?depth_1-utm_source=distribute.pc_releva ...