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 ...
随机推荐
- iOS 8 AutoLayout与Size Class
转自:http://www.cocoachina.com/ios/20141217/10669.html 前言 iOS8 和iPhone6发布已经过去蛮久了,广大的果粉终于迎来了大屏iPhone,再也 ...
- 1.4 算法 - algorithm
1)概述 2)示例 //algorithm find演示 #include <vector> #include <algorithm> #include <iostrea ...
- mysql 三个表连接查询
权限表(permission)10 字段名称 类型 约束 描述 authorityid integer Pk not null 权限流水号id PK userNameId int not nul ...
- 加载gif动画的三种方式
GifView.h/** * 调用结束就开始播放动画,如果需要用户指定何时播放的话,只需要把timer的开始放到合适的位置.通过对CFDictonaryRaf 也就是gifProperties的改变, ...
- php文件上传进度条例子
<?php session_start(); ?> <!DOCTYPE html> <html lang="zh-CN"> <head&g ...
- ASP.NET MVC中Controller返回值类型ActionResult
1.返回ViewResult视图结果,将视图呈现给网页 public class TestController : Controller { //必须存在Controller\Test\Index.c ...
- Linux系统启动过程分析
[原创]Linux系统启动过程分析-wjlkoorey258-ChinaUnix博客http://blog.chinaunix.net/uid-23069658-id-3142047.html 经过对 ...
- Pyqt 时时CPU使用情况
借鉴代码来自:https://github.com/hgoldfish/quickpanel 实现代码: # -*- coding:utf-8 -*- from __future__ import p ...
- n的阶乘高精度算法【阶乘】
C语言实验——求阶乘(循环结构) Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu ...
- mysql 如何设置自动增长序列 sequence(一)
背景:由于项目需要,必须用mysql设置主键自增长,而且想用字符串的.经过上网查找并且实验,终于做出了一套方案.现在就共享给大家! 解决思路:由于mysql不带sequence,所以要手写的,创建一张 ...