poj 3261 Milk Patterns(后缀数组-最长重复子串)

Description

Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he discovered that although he can't predict the quality of milk from one day to the next, there are some regular patterns in the daily milk quality.

To perform a rigorous study, he has invented a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 inclusive, and has recorded data from a single cow over N (1 ≤ N ≤ 20,000) days. He wishes to find the longest pattern of samples which repeats identically at least K (2 ≤ K ≤ N) times. This may include overlapping patterns -- 1 2 3 2 3 2 3 1 repeats 2 3 2 3 twice, for example.

Help Farmer John by finding the longest repeating subsequence in the sequence of samples. It is guaranteed that at least one subsequence is repeated at least K times.

Input

Line 1: Two space-separated integers: N and K 
Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.

Output

Line 1: One integer, the length of the longest pattern which occurs at least K times

Sample Input

8 2
1
2
3
2
3
2
3
1

Sample Output

4

题意:给你一个n,k,一个长度为n的串,问你最长至少重复k次的子串的长度是多少。这题里的重复的子串是可以叠加的,就是重复的这些子串是可以有重合部分的。

我们可以二分这个答案,然后判定这个答案合不合法。那么问题就是怎么判定答案合不合法的问题了。

我们知道高度数组的值是相邻排名的两个后缀的最长公共前缀的长度,两个不相邻的后缀的最长公共前缀的长度就是这两个排名之间高度数组的最小值,那么要计算一个子串重复了多少次,那么只用计算一个连续的都大于我们二分出的值的区间的长度。

对于这个样例,按排名把后缀排序:

12323231
231      
23231      
2323231   
31 
3231 
323231 

对于二分出来的x==3,我们发现最后两个后缀是满足重复次数大于等于k的,对于二分出来的x==4,第三到第四个后缀是满足重复次数大于等于k的。

#include <cstdio>
#include <cstring>
#include<iostream>
#include <algorithm>

using namespace std;

const int MAXN = 200005;


int SA[MAXN], Rank[MAXN], Height[MAXN], tax[MAXN], tp[MAXN], a[MAXN], n, m;
char str[MAXN];

void RSort()
{
    for (int i = 0; i <= m; i ++) tax[i] = 0;
    for (int i = 1; i <= n; i ++) tax[Rank[tp[i]]] ++;
    for (int i = 1; i <= m; i ++) tax[i] += tax[i-1];
    for (int i = n; i >= 1; i --) SA[tax[Rank[tp[i]]] --] = tp[i];
}
int cmp(int *f, int x, int y, int w) { return (f[x] == f[y] && f[x + w] == f[y + w]); }


void Suffix()
{
    for (int i = 1; i <= n; i ++) Rank[i] = a[i], tp[i] = i;
    m = 256 ,RSort();

    for (int w = 1, p = 1, i; p < n; w += w, m = p)
    {
        for (p = 0, i = n - w + 1; i <= n; i ++) tp[++ p] = i;
        for (i = 1; i <= n; i ++) if (SA[i] > w) tp[++ p] = SA[i] - w;

        RSort(), swap(Rank, tp), Rank[SA[1]] = p = 1;

        for (i = 2; i <= n; i ++) Rank[SA[i]] = cmp(tp, SA[i], SA[i - 1], w) ? p : ++ p;
    }

    int j, k = 0;
    for(int i = 1; i <= n; Height[Rank[i ++]] = k)
        for( k = k ? k - 1 : k, j = SA[Rank[i] - 1]; a[i + k] == a[j + k]; ++ k);
}


void Init()
{
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    Suffix();
}
int k;

bool judge(int x)
{
    int i = 1;
    while(i <= n)
    {
        int j = i+1;
        int cnt = 1;
        while(Height[j] >= x && j <= n)
        {
            cnt++;
            j++;
        }
        if(cnt >= k)
            return true;
        i = j;
    }

    return false;
}
int binarysearch()
{
    int l = 1,r = n;
    int ans = l;
    while(l <= r)
    {
        int mid = (l+r)/2;
        if(judge(mid))
        {
            l = mid + 1;
            ans = mid;
        }
        else
            r = mid - 1;
    }

    return ans;
}
int main(void)
{
    while(scanf("%d%d",&n,&k)==2)
    {
        Init();
        int ans = binarysearch();
        printf("%d\n",ans);
    }

    return 0;
}


内容概要:本文档是一份关于交换路由配置的学习笔记,系统地介绍了网络设备的远程管理、交换机与路由器的核心配置技术。内容涵盖Telnet、SSH、Console三种远程控制方式的配置方法;详细讲解了VLAN划分原理及Access、Trunk、Hybrid端口的工作机制,以及端口镜像、端口汇聚、端口隔离等交换技术;深入解析了STP、MSTP、RSTP生成树协议的作用与配置步骤;在路由部分,涵盖了IP地址配置、DHCP服务部署(接口池与全局池)、NAT转换(静态与动态)、静态路由、RIP与OSPF动态路由协议的配置,并介绍了策略路由和ACL访问控制列表的应用;最后简要说明了华为防火墙的安全区域划分与基本安全策略配置。; 适合人群:具备一定网络基础知识,从事网络工程、运维或相关技术岗位1-3年的技术人员,以及准备参加HCIA/CCNA等认证考试的学习者。; 使用场景及目标:①掌握企业网络中常见的交换与路由配置技能,提升实际操作能力;②理解VLAN、STP、OSPF、NAT、ACL等核心技术原理并能独立完成中小型网络搭建与调试;③通过命令示例熟悉华为设备CLI配置逻辑,为项目实施和故障排查提供参考。; 阅读建议:此笔记以实用配置为主,建议结合模拟器(如eNSP或Packet Tracer)动手实践每一条命令,对照拓扑理解数据流向,重点关注VLAN间通信、路由选择机制、安全策略控制等关键环节,并注意不同设备型号间的命令差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值