PAT (Top Level) Practise 1005 Programming Pattern (35)
后缀数组。排序之后得到height数组,然后从上到下将height>=len的都分为一组,然后找到第一组个数最多的输出即可。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int maxn=+; int wa[maxn],wb[maxn],wv[maxn],WS[maxn];
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=; i<m; i++) WS[i]=;
for(i=; i<n; i++) WS[x[i]=r[i]]++;
for(i=; i<m; i++) WS[i]+=WS[i-];
for(i=n-; i>=; i--) sa[--WS[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
for(p=,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<n; i++) wv[i]=x[y[i]];
for(i=; i<m; i++) WS[i]=;
for(i=; i<n; i++) WS[wv[i]]++;
for(i=; i<m; i++) WS[i]+=WS[i-];
for(i=n-; i>=; i--) sa[--WS[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
return;
} int Rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
int i,j,k=;
for(i=; i<=n; i++) Rank[sa[i]]=i;
for(i=; i<n; height[Rank[i++]]=k)
for(k?k--:,j=sa[Rank[i]-]; r[i+k]==r[j+k]; k++);
return;
} int T,n,a[maxn],SA[maxn],sum[maxn];
char str[maxn],op[]; int Find(int D,int l,int r)
{
int pos=-;
while(l<=r)
{
int mid=(l+r)/;
if(sum[mid]-D>) r=mid-;
else if(sum[mid]-D==) pos=mid,r=mid-;
else l=mid+;
}
return pos;
} int main()
{
int len; scanf("%d",&len); getchar();
gets(str); n=strlen(str);
for(int i=;i<n;i++) a[i]=(int)str[i];
a[n]=; da(a,SA,n+,); calheight(a,SA,n);
int L=,R=;
int ansL,ansR,ans=;
while()
{
if(height[R]>=len)
{
R++;
if(R-L+>ans) ans=R-L+, ansL = L, ansR = R;
}
else L=R+, R=L;
if(L>n||R>n) break;
}
for(int i=SA[ansL];i<=SA[ansL]+len-;i++) printf("%c",str[i]);
printf(" %d\n",ans);
return ;
}
PAT (Top Level) Practise 1005 Programming Pattern (35)的更多相关文章
- PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)
1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...
- PAT TOP 1005 Programming Pattern (35 分)哈希做法
1005 Programming Pattern (35 分) Programmers often have a preference among program constructs. For ex ...
- PAT (Basic Level) Practise (中文)-1039. 到底买不买(20)
PAT (Basic Level) Practise (中文)-1039. 到底买不买(20) http://www.patest.cn/contests/pat-b-practise/1039 小红 ...
- PAT (Basic Level) Practise (中文)- 1022. D进制的A+B (20)
PAT (Basic Level) Practise (中文)- 1022. D进制的A+B (20) http://www.patest.cn/contests/pat-b-practise/1 ...
- PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)
PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...
- PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)
PAT (Basic Level) Practise (中文)-1025. 反转链表 (25) http://www.patest.cn/contests/pat-b-practise/1025 ...
- PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)
PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15) http://www.patest.cn/contests/pat-b-practise/10 ...
- PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)
PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20) http://www.patest.cn/contests/pat-b-practise/1027 本题 ...
- PAT (Basic Level) Practise (中文)-1028. 人口普查(20)
PAT (Basic Level) Practise (中文)-1028. 人口普查(20) http://www.patest.cn/contests/pat-b-practise/1028 某 ...
随机推荐
- Java中的注释
代码注释是架起程序设计者与程序阅读者之间的通信桥梁,最大限度的提高团队开发合作效率.也是程序代码可维护性的重要环节之一.所以我们不是为写注释而写注释.下面说一下Javadoc注释规范以及楼主在J2EE ...
- LeetCode 328. Odd Even Linked List C#
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- 学习node的REPL
REPL: read eval print loop 支持 多行代码 编辑模式,即 识别{} () 为代码块. 在repl中快速查看 对象的属性 gl = global repl command .h ...
- scala优点以及eclipse上安装scala插件
可拓展 (面向对象,函数式编程) 静态类型化 (可检验,安全重构) 兼容JAVA (类库调用,互操作) 支持并发控制 (强计算能力,自定义其他控制结构) 语法简洁 (代码行短,类型推断,抽象控制) 插 ...
- .net中读取xml文件中节点的所有属性信息
功能描述: 将数据以xml的格式记录成配置文件,需要获取配置文件中的数据时,则获取对应的配置文件,读取配置文件里对应节点的所有属性. 逻辑实现: 1.将数据配置好在xml文件中. 2.获取xml文件中 ...
- Java继承多态中的方法访问权限控制
java中的方法天生具有继承多态特性,这点与C++有很大不同(需要在父类方发上加virtual关键字),但用起来确实方便了许多. 最简单的继承多态 声明一个接口BaseIF,只包含一个方法声明 pub ...
- c++ 定义宏常量
#ifndef __GAME_DATA_H__ #define __GAME_DATA_H__ #ifndef GAME_IS_CREATE_SHOP #define GAME_IS_CREATE_S ...
- 设置phpMyAdmin本地自动登陆
一般配置本地测试用的 phpMyAdmin 可以不用每次输入帐号密码,打开后自动登陆就行了. 版本: phpMyAdmin 3.5.3 打开: phpMyAdmin 根目录 复制: config.sa ...
- Effective JavaScript :第三章
1.函数调用.方法调用以及构造函数调用只是单个构造对象的三种不同的使用模式. 第一种函数调用模式: function hello(username){ return ‘hello,’+ usernam ...
- dplyr 数据操作 统计描述(summarise)
在R中,summary()是一个基础包中的重要统计描述函数,同样的在dplyr中summarise()函数也可以对数据进行统计描述. 不同的是summarise()更加的灵活多变,下面来看下summa ...