PAT T1005 Programming Pattern
建立后缀数组,遍历height数组找到连续大于len的最长子序列~
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e7+;
char s[maxn];
int n;
int rk[maxn];
int sa[maxn];
int height[maxn];
int tmp[maxn];
int c[maxn];
int t1[maxn];
int t2[maxn];
void build_sa (char s[],int n,int m) {
int i,j,p;
int *x=t1;
int *y=t2;
for (i=;i<m;i++) c[i]=;
for (i=;i<n;i++) c[x[i]=s[i]]++;
for (i=;i<m;i++) c[i]+=c[i-];
for (i=n-;i>=;i--) sa[--c[x[i]]]=i;
for (j=;j<=n;j<<=) {
p=;
for (i=n-j;i<n;i++) y[p++]=i;
for (i=;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
for (i=;i<m;i++) c[i]=;
for (i=;i<n;i++) c[x[y[i]]]++;
for (i=;i<m;i++) c[i]+=c[i-];
for (i=n-;i>=;i--) sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=;
x[sa[]]=;
for (i=;i<n;i++)
x[sa[i]]=y[sa[i-]]==y[sa[i]]&&y[sa[i-]+j]==y[sa[i]+j]?p-:p++;
if (p>=n) break;
m=p;
}
}
void getHeight (char s[],int n) {
int i,j,k=;
for (i=;i<=n;i++) rk[sa[i]]=i;
for (i=;i<n;i++) {
if (k) k--;
j=sa[rk[i]-];
while (s[i+k]==s[j+k]) k++;
height[rk[i]]=k;
}
}
int main () {
int len;
scanf ("%d",&len);
getchar ();
cin.getline (s,maxn);
n=strlen(s);
build_sa (s,n+,);
getHeight (s,n);
int cnt=;
int res=;
int ans=;
for (int i=;i<=n;i++) {
if (height[i]>=len) ans++;
else ans=;
if (sa[i]+len<=n&&cnt<ans) {
cnt=ans;
res=sa[i];
}
}
for (int i=;i<len;i++)
printf ("%c",s[res+i]);
printf (" %d",cnt);
return ;
}
PAT T1005 Programming Pattern的更多相关文章
- PAT TOP 1005 Programming Pattern (35 分)哈希做法
1005 Programming Pattern (35 分) Programmers often have a preference among program constructs. For ex ...
- PAT (Top Level) Practise 1005 Programming Pattern (35)
后缀数组.排序之后得到height数组,然后从上到下将height>=len的都分为一组,然后找到第一组个数最多的输出即可. #pragma comment(linker, "/STA ...
- Game Programming Pattern
http://gameprogrammingpatterns.com/contents.html
- lazy ideas in programming
lazy形容词,懒惰的,毫无疑问是一个贬义词.但是,对于计算机领域,lazy却是非常重要的优化思想:把任务推迟到必须的时刻,好处是避免重复计算,甚至不计算.本文的目的是抛砖引玉,总结一些编程中的laz ...
- lazy ideas in programming(编程中的惰性思想)
lazy形容词,懒惰的,毫无疑问是一个贬义词.但是,对于计算机领域,lazy却是非常重要的优化思想:把任务推迟到必须的时刻,好处是避免重复计算,甚至不计算.本文的目的是抛砖引玉,总结一些编程中的laz ...
- Linux System Programming 学习笔记(七) 线程
1. Threading is the creation and management of multiple units of execution within a single process 二 ...
- PAT甲级——1025 PAT Ranking
1025 PAT Ranking Programming Ability Test (PAT) is organized by the College of Computer Science and ...
- Awesome Go
A curated list of awesome Go frameworks, libraries and software. Inspired by awesome-python. Contrib ...
- Go 语言相关的优秀框架,库及软件列表
If you see a package or project here that is no longer maintained or is not a good fit, please submi ...
随机推荐
- 吴裕雄 python 机器学习——数据预处理正则化Normalizer模型
from sklearn.preprocessing import Normalizer #数据预处理正则化Normalizer模型 def test_Normalizer(): X=[[1,2,3, ...
- 吴裕雄 python 机器学习——数据预处理二元化Binarizer模型
from sklearn.preprocessing import Binarizer #数据预处理二元化Binarizer模型 def test_Binarizer(): X=[[1,2,3,4,5 ...
- windows 动态库导出
以下内容来自博客:https://blog.csdn.net/fengbingchun/article/details/78825004 __declspec是Microsoft VC中专用的关键字, ...
- 【PAT甲级】1107 Social Clusters (30分)(非递归并查集)
题意: 输入一个正整数N(<=1000),表示人数,接着输入N行每行包括一个他的爱好数量:和爱好的序号.拥有相同爱好的人们可以默认他们在同一个俱乐部,输出俱乐部的数量并从大到小输出俱乐部的人数( ...
- 【转】VMware Converter迁移linux系统虚拟机
原始出处 今天接到一个需求,迁移一台linux的业务系统到vCenter云管理平台,其中遇到一些问题,于是进行了排错,这个过程与大家分享,下面把整个步骤进行截图说明. 1. 首先,登录到VMware ...
- pwnable.kr-balckjack-Writeup
MarkdownPad Document *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...
- opencv:opencv概述
opencv官方:www.opencv.org github:https://github.com/opencv OpenCV OpenCV是一个开放源代码的计算机视觉应用平台,由英特尔公司研发中心俄 ...
- cent os 7.3修改mac地址方法
一.修改MAC地址方法 linux环境下: 需要用 #ifconfig eth0 down 先把网卡禁用 再用ifconfig eth0 hw ether 1234567890ab ...
- Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/tools/ant/launch/Launcher : Unsupported major.min
有事这么一大串错误 1.版本问题 首先看咱们的jdk安装的版本,我装的是1.7.0,但是ant下的是1.10.0版本,换成1.7.0就ok了 官网下载http://ant.apache.org 所有版 ...
- 【PAT甲级】1069 The Black Hole of Numbers (20 分)
题意: 输入一个四位的正整数N,输出每位数字降序排序后的四位数字减去升序排序后的四位数字等于的四位数字,如果数字全部相同或者结果为6174(黑洞循环数字)则停止. trick: 这道题一反常态的输入的 ...