BZOJ1170 : [Balkan2007]Cipher
首先对于每个位置,求出它开始长度为y的横行的hash值,然后对于hash值再求一次竖列的hash值,排序后求出众数即可。
时间复杂度$O(n^2\log n)$。
#include<cstdio>
#include<algorithm>
#define N 1010
typedef unsigned long long ll;
const ll D1=197,D2=131;
int n,m,x,y,i,j,ans,t,cnt;char a[N][N];ll pow1[N],pow2[N],h[N][N],tmp;
struct P{int x,y;ll z;P(){}P(int _x,int _y,ll _z){x=_x,y=_y,z=_z;}}q[N*N],fin[N];
inline bool cmp(const P&a,const P&b){return a.z<b.z;}
inline bool cmp2(const P&a,const P&b){return a.x==b.x?a.y<b.y:a.x<b.x;}
int main(){
scanf("%d%d",&n,&m);gets(a[0]);
for(i=1;i<=n;i++)gets(a[i]+1);
scanf("%d%d",&x,&y);
for(pow1[0]=pow2[0]=i=1;i<=n||i<=m;i++)pow1[i]=pow1[i-1]*D1,pow2[i]=pow2[i-1]*D2;
for(i=1;i<=n;i++){
for(tmp=0,j=1;j<y;j++)tmp=tmp*D1+a[i][j],h[i][j]=0;
for(j=y;j<=m;j++)h[i][j]=tmp=tmp*D1-pow1[y]*a[i][j-y]+a[i][j];
}
for(t=0,i=y;i<=m;i++){
for(tmp=0,j=1;j<x;j++)tmp=tmp*D2+h[j][i];
for(j=x;j<=n;j++)q[++t]=P(j-x+1,i-y+1,tmp=tmp*D2-pow2[x]*h[j-x][i]+h[j][i]);
}
for(std::sort(q+1,q+t+1,cmp),j=1,i=2;i<=t;i++)if(q[i-1].z!=q[i].z){
if(i-j>ans)ans=i-j,tmp=q[j].z;
j=i;
}
if(t-j+1>ans)ans=t-j+1,tmp=q[t].z;
printf("%d %d\n",x,y);
for(i=1;i<=t;i++)if(q[i].z==tmp)fin[++cnt]=P(q[i].x,q[i].y,0);
std::sort(fin+1,fin+cnt+1,cmp2);
for(i=0;i<x;puts(""),i++)for(j=0;j<y;j++)putchar(a[fin[1].x+i][fin[1].y+j]);
for(printf("%d\n",cnt),i=1;i<=cnt;i++)printf("%d %d\n",fin[i].x,fin[i].y);
return 0;
}
BZOJ1170 : [Balkan2007]Cipher的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- BZOJ 1176: [Balkan2007]Mokia
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 2012 Solved: 896[Submit][St ...
- JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher
加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...
- POJ1026 Cipher(置换的幂运算)
链接:http://poj.org/problem?id=1026 Cipher Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6014 Solved: 2503[Submit ...
- freeCodeCamp:Caesars Cipher
让上帝的归上帝,凯撒的归凯撒. 下面我们来介绍风靡全球的凯撒密码Caesar cipher,又叫移位密码. 移位密码也就是密码中的字母会按照指定的数量来做移位. 一个常见的案例就是ROT13密码,字母 ...
- ural Cipher Message
Cipher Message Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- 紫书例题-Ancient Cipher
Ancient Roman empire had a strong government system with various departments, including a secret ser ...
- 【BZOJ-1031】字符加密Cipher 后缀数组
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5504 Solved: 2277[Submit ...
随机推荐
- bzoj千题计划270:bzoj4559: [JLoi2016]成绩比较(拉格朗日插值)
http://www.lydsy.com/JudgeOnline/problem.php?id=4559 f[i][j] 表示前i门课,有j个人没有被碾压的方案数 g[i] 表示第i门课,满足B神排名 ...
- History of Monte Carlo Methods - Part 1
History of Monte Carlo Methods - Part 1 Some time ago in June 2013 I gave a lab tutorial on Monte Ca ...
- Algorithm(1) - Karatsuba multiplication
这个系列主要是记一下目前效率较高或者比较出名的一些算法. Karatsuba multiplication: x=5678 then: a=56 b=67 y=1234 c= ...
- springMvc + Maven 项目提示 hessian 依赖包 无法下载;
首先 从 https://github.com/alibaba/dubbo/archive/master.zip 下载最新的 dubbo 源码包到本地某个目录, 解压出来: cmd 进入该目录: 执行 ...
- 20155338 2016-2017-2 《Java程序设计》第7周学习总结
20155338 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 本周学习了第十二章和第十三章的内容,我重点学习了第十三章时间与日期的相关内容. 时间的度量: ...
- 第13月第16天 ios keywindow
1. 在弹出层弹出后keywindow已改变 http://www.jianshu.com/p/4695d7efa20b
- HTTP::UserAgent注意问题
例用 HTTP::Request 设置头信息时, 比如 add-content , 第二次再执行 add-content 时, content 内容会追加, 并不会重新添加. 当下次再 add-con ...
- java future 并发简单实现
List<Future<>> futures = new ArrayList<>();List<T> t= new ArrayList<>( ...
- 009_【OS X和iOS系统学习笔记】 OS X架构
1.OS X是整个操作系统的集体名称,而Darwin是其中的一个组件. 2.Darwin是操作系统的类UNIX核心,本身由内核.XNU和运行时组成. 3.uname指令:可以得到有关架构的详细信息以及 ...
- nginx安装报错:configure: error: the HTTP rewrite module requires the PCRE library
参考:http://blog.51cto.com/williamx/958398 需要安装pcre-devel与openssl-devel yum -y install pcre-devel open ...