poj3261Milk Patterns(可重叠的k次最长重复子串)

本文介绍了一种寻找序列中最长重复子序列的算法,该算法适用于分析奶牛产奶质量的周期性变化规律,通过后缀数组和二分查找的方法,在O(nlogn)的时间复杂度内解决问题。
Milk Patterns
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 12401 Accepted: 5490
Case Time Limit: 2000MS

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 ≤ KN) 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

Source

USACO 2006 December Gold
先二分答案,然后将后缀分成若干组。不
同的是,这里要判断的是有没有一个组的后缀个数不小于 k。如果有,那么存在
k 个相同的子串满足条件,否则不存在。这个做法的时间复杂度为 O(nlogn)。

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=20000+100;
int n,k;
struct node{
    int num;
    int id;
}a[maxn];

int s[maxn];
int sa[maxn],t[maxn],t2[maxn],c[maxn];
//构造字符串s的后缀数组,每个字符值必须为0~m-1
void build_sa(int m){
    int *x=t,*y=t2;
    //基数排序
    for(int i=0;i<m;i++)    c[i]=0;
    for(int i=0;i<n;i++)    c[x[i]=s[i]]++;
    for(int i=1;i<m;i++)    c[i]+=c[i-1];
    for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
    for(int k=1;k<=n;k<<=1){
        int p=0;
        //直接利用sa数组排序第二关键字
        for(int i=n-k;i<n;i++)  y[p++]=i;
        for(int i=0;i<n;i++)    if(sa[i]>=k)    y[p++]=sa[i]-k;
        //基数排序第一关键字
        for(int i=0;i<m;i++)    c[i]=0;
        for(int i=0;i<n;i++)    c[x[y[i]]]++;
        for(int i=1;i<m;i++)    c[i]+=c[i-1];
        for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
        //根据sa和y计算新的x数组
        swap(x,y);
        p=1;
        x[sa[0]]=0;
        for(int i=1;i<n;i++)
            x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
        if(p>=n)
            break;
        m=p;                //下次基数排序的最大值
    }
}
int rank1[maxn],height[maxn];

void getHeight(){
    int i,j,k=0;
    for(i=0;i<=n;i++)    rank1[sa[i]]=i;
    for(i=0;i<n;i++){
        if(k)
            k--;
        int j=sa[rank1[i]-1];
        while(s[i+k]==s[j+k])   k++;
        height[rank1[i]]=k;
    }
}

bool check(int m){
    int cnt=0;
    for(int i=0;i<=n;i++){
        if(height[i]>=m){
            cnt++;
            if(cnt>=k-1)
                return true;
        }
        else
            cnt=0;
    }
    return false;
}

bool cmp(node b,node d){
    return b.num<d.num;
}

int main(){
    while(scanf("%d%d",&n,&k)!=EOF){
        for(int i=0;i<n;i++){
            scanf("%d",&a[i].num);
            a[i].id=i;
            a[i].num++;
        }
        sort(a,a+n,cmp);
        int cnt=1;
        s[a[0].id]=1;
        for(int i=1;i<n;i++){
            if(a[i].num==a[i-1].num)
                s[a[i].id]=s[a[i-1].id];
            else
                s[a[i].id]=s[a[i-1].id]+1;
        }
        //for(int i=0;i<n;i++)
            //printf("%d\n",s[i]);
        s[n++]=0;
        build_sa(n);
        n--;
        getHeight();
        int low=1,high=n;
        while(high-low>=0){
            int mid=(low+high)>>1;
            if(check(mid))
                low=mid+1;
            else
                high=mid-1;
        }
        printf("%d\n",high);
    }
    return 0;
}
/*
4 2
1 3 1 4
8 2
1 2 3 2 3 2 3 1
*/


基于51单片机,实现对直流电机的调速、测速以及正反转控制。项目包含完整的仿真文件、源程序、原理图和PCB设计文件,适合学习和实践51单片机在电机控制方面的应用。 功能特点 调速控制:通过按键调整PWM占空比,实现电机的速度调节。 测速功能:采用霍尔传感器非接触式测速,实时显示电机转速。 正反转控制:通过按键切换电机的正转和反转状态。 LCD显示:使用LCD1602液晶显示屏,显示当前的转速和PWM占空比。 硬件组成 主控制器:STC89C51/52单片机(与AT89S51/52、AT89C51/52通用)。 测速传感器:霍尔传感器,用于非接触式测速。 显示模块:LCD1602液晶显示屏,显示转速和占空比。 电机驱动:采用双H桥电路,控制电机的正反转和调速。 软件设计 编程语言:C语言。 开发环境:Keil uVision。 仿真工具:Proteus。 使用说明 液晶屏显示: 第一行显示电机转速(单位:转/分)。 第二行显示PWM占空比(0~100%)。 按键功能: 1键:加速键,短按占空比加1,长按连续加。 2键:减速键,短按占空比减1,长按连续减。 3键:反转切换键,按下后电机反转。 4键:正转切换键,按下后电机正转。 5键:开始暂停键,按一下开始,再按一下暂停。 注意事项 磁铁和霍尔元件的距离应保持在2mm左右,过近可能会在电机转动时碰到霍尔元件,过远则可能导致霍尔元件无法检测到磁铁。 资源文件 仿真文件:Proteus仿真文件,用于模拟电机控制系统的运行。 源程序:Keil uVision项目文件,包含完整的C语言源代码。 原理图:电路设计原理图,详细展示了各模块的连接方式。 PCB设计:PCB布局文件,可用于实际电路板的制作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值