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 ...
随机推荐
- Mac 下查看 Android Studio 的 SHA1的方法
cmd -> ->cd .android ->keytool -v -list -keystore debug.keystore 默认口令:android ************* ...
- 写了个简单的pdo的封装类
<?php class PD { //造对象 public $dsn = "mysql:dbname=test2;host=localhost"; //数据库类型,数据库名和 ...
- Android浏览本地 API文档 + 解决页面加载慢的问题
火狐浏览器安装离线浏览插件: 用浏览器打开index.html文件,你会发现加载的很慢,原因你懂的,为此,我们可以通过离线的方式 查看本地API文档,用火狐浏览器 + Work Offline插 ...
- .NET NLog 详解(五) - Condition Expression
Sample <!-- during normal execution only log Info messages --> <defaultFilter>level > ...
- 吐个槽,对VB6.0 还有VBS 说ByeBye
往事不堪回首,折腾了个把月的老系统,心中郁结,不吐不快.系统架构是ASP +VBS +VB6.0 + SQL Server2000, 第一个版本开发完成大概是在2000年.基本是处于交接无力,看代码就 ...
- C专家编程cdecl
理解所有分析过程的代码段 Page71(中文版) 你可以轻松地编写一个能够分析C语言的声明并把他们翻译成通俗语言的程序.事实上,为什么不?C语言声明的基本形式已经描述清楚.我们所需要的只是编写一段能够 ...
- ListView遍历每个Item出现NullPointerException的异常(转)
在使用ListView过程中我们有时候需要遍历取得每个Item项中的一些数据(比如每个Item里面有TextView,需要获取它的文本等等),但是我们在遍历过程中经常会遇到NullPointerExc ...
- banner无限轮播
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayou ...
- bat 炸弹升级
转自:http://digi.163.com/15/0320/06/AL4LP0QD0016192R.html 第1页:什么是批处理炸弹? 最近网上流传一个叫做<大哥别杀我>视频纷纷遭到网 ...
- Android 自定义title 之Action Bar
Android 自定义title 之Action Bar 2014-06-29 飞鹰飞龙... 摘自 博客园 阅 10519 转 25 转藏到我的图书馆 微信分享: Action Ba ...