BZOJ3084 : [Algorithmic Engagements 2011]The Shortest Period
枚举答案长度$L$,设$A$和$B$分别为第一个循环节和反串的第一个循环节。
1.坏点不在$A$,那么可以暴力匹配检验。
2.坏点不在$B$,那么把串翻转后不在$A$中,转化为1。
3.坏点在$A$和$B$的交里面,那么只要长度为$n-L+1$的前后缀相同,那么就存在长度为$L$的循环节。
通过扩展kmp和Hash快速判断即可,时间复杂度$O(dn\log n)$。
#include<cstdio>
const int N=200010,P=233;
int T,n,i,j,t,k,p,l,ans,g[N];char a[N];unsigned int pow[N],f[N];
inline void swap(char&a,char&b){char c=a;a=b;b=c;}
inline unsigned int hash(int l,int r){return f[r]-f[l-1]*pow[r-l+1];}
inline int min(int a,int b){return a<b?a:b;}
void solve(){
for(i=1;i<=n;i++)f[i]=f[i-1]*P+a[i];
for(g[i=0]=n;i<n-1&&a[i+1]==a[i+2];i++);
for(g[t=1]=i,k=2;k<n;k++){
p=t+g[t]-1,l=g[k-t];
if(k+l>p){
j=(p-k+1)>0?(p-k+1):0;
while(k+j<n&&a[k+j+1]==a[j+1])j++;
g[k]=j,t=k;
}else g[k]=l;
}
for(i=n;i;i--)g[i]=g[i-1];
for(i=1;i<ans;i++){
j=g[i+1];
if(j==n-i||g[i+2]>=n-i+1)ans=i;else{
j+=i+2,k=(j-2)/i*i+1,t=k+i;
if(t>n)t=n;
if(hash(j,t)!=hash(j-k,t-k)||g[t+1]<n-t)continue;
for(t++;t<=n;t+=i)if(g[t]<min(i,n-t+1))break;
if(t>n)ans=i;
}
}
}
int main(){
for(pow[0]=i=1;i<N;i++)pow[i]=pow[i-1]*P;
scanf("%d",&T);
while(T--){
scanf("%d%s",&n,a+1);
ans=n-1;
solve();
for(i=1;i<n-i+1;i++)swap(a[i],a[n-i+1]);
solve();
printf("%d\n",ans);
}
return 0;
}
BZOJ3084 : [Algorithmic Engagements 2011]The Shortest Period的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- About Adultism and why things ar the way they are
About - Adultism About Adultism and why things ar the way they are In this page we will try to clari ...
- 【HDU 5855】Less Time, More profit(网络流、最小割、最大权闭合子图)
Less Time, More profit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- HDU5855 Less Time, More profit(最大权闭合子图)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5855 Description The city planners plan to build ...
- eclipse配置maven + 创建maven项目
登录|注册 努力+坚持,而且还很年轻 目录(?)[+] 在现实的企业中,以低成本.高效率.高质量的完成项目,不仅仅需要技术大牛,企业更加需要管理大牛,管理者只懂技术是远远不够的.当 ...
- 转 --maven系列之一 简介
http://blog.csdn.net/jiuqiyuliang/article/details/41076215 [项目管理和构建]——Maven简介(一) 2015-01-31 21:27 68 ...
- Maven简介(一)
在现实的企业中,以低成本.高效率.高质量的完成项目,不仅仅需要技术大牛,企业更加需要管理大牛,管理者只懂技术是远远不够的.当然,管理可以说有很多的方面,例如:对人员的管理,也有对项目的管理等等.如果你 ...
- Java8新特性_日期时间新类 LocalDate、LocalTime、LocalDateTime
import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeForma ...
- 关于ADO.Net SqlConnection的性能优化
Connections Database connections are an expensive and limited resource. Your approach to connection ...
随机推荐
- Python lambda函数使用
- Loadrunner11.0 录制手机App脚本的方法
使用Loadrunner录制手机终端App脚本 1. 说明 目前手机APP上的功能日益丰富,对手机应用功能的性能测试需求也越来越多.公司比较抠门没有花钱买Loadrunner,可怜我们工作中一直用的破 ...
- Ubuntu 14.04 Trusty安装java环境
原文:Install Oracle Java 6, 7, or 8 in Ubuntu 14.04 Trusty 命令如下: sudo add-apt-repository ppa:webupd8te ...
- jQuery - 9.Ajax
9.1 Ajax 的 XMLHttpRequest 对象 9.2 JQuery中的Ajax 9.2.1 load()方法 9.2.2 $.get() 9.2.3 $.post() 9.2.4 $.ge ...
- 关于配置Spring框架的多个propertyConfigurer的问题
http://blog.csdn.net/aa427/article/details/38375259
- Tensorflow 的Word2vec demo解析
简单demo的代码路径在tensorflow\tensorflow\g3doc\tutorials\word2vec\word2vec_basic.py Sikp gram方式的model思路 htt ...
- DC/DC与LDO的差别
转自:http://bbs.eetop.cn/thread-459121-1-1.html 在平时的学习中,我们都有接触LDO和DC/DC这一类的电源产品,但作为学生的我们队这些东西可能了解不够深刻, ...
- yaf框架使用(centos6.5)
安装好php环境之后 安装扩展包 $yum install php-devel /usr/bin/ 就会出现phpize工具包 下载yaf-2.2.8.gz源文件,解压后,进入源文件 phpize [ ...
- Mac系统修改Intellij Idea默认JDK版本
Intellij IDEA 默认情况下,使用的jdk的版本是1.6,当第一次启动IDEA的时候,如果系统中未安装jdk,则系统会自动到苹果官网下载jdk安装文件.如果你的系统已经安装了jdk1.7或是 ...
- 制作U盘启动系统盘
下载ULtraISO,安装之后,先打开一个iso系统文件,然后选中菜单“启动”下的“写入硬盘映像”