最小表示法

本文介绍了一种使用双指针技巧寻找字符串最小表示法的高效算法,通过不断比较并移动指针,最终找到最小子串,算法的时间复杂度为O(n)。

好像还没有写过最小表示法以前都是用SAM写的
用两个指针\(i\)\(j\)指向开头两个不同的位置,然后找到它们往后第一个不同的地方\(i + k\),比较大小
较大的移动指针\(k + 1\)
直至到达末尾或者\(k = len\)

由于两指针移动次数和比较次数时同阶的,所以复杂度是\(O(n)\)
BZOJ2882

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
using namespace std;
const int maxn = 300005,maxm = 100005,INF = 0x3f3f3f3f;
inline int read(){
    int out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57){if (c == '-') flag = 0; c = getchar();}
    while (c >= 48 && c <= 57){out = (out << 1) + (out << 3) + c - 48; c = getchar();}
    return flag ? out : -out;
}
int A[maxn],n,pos;
void work(){
    int i = 0,j = 1,k = 0,t;
    while (i < n && j < n && k < n){
        t = A[(i + k) % n] - A[(j + k) % n];
        if (!t) k++;
        else {
            if (t > 0) i += k + 1;
            else j += k + 1;
            if (i == j) j++;
            k = 0;
        }
    }
    pos = i < j ? i : j;
}
int main(){
    n = read();
    for (int i = 0; i < n; i++) A[i] = read();
    work();
    for (int i = 0; i < n; i++) printf("%d ",A[(pos + i) % n]);
    return 0;
}

转载于:https://www.cnblogs.com/Mychael/p/9269833.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值