【LOJ #129】【模板】 Lyndon 分解

传送门

由于 L L L串是不增的
可以发现一定连续一段 L L L串相同
于是考虑先分割成单减的,然后每一部分按循环节分割
考虑假设已经求出了 1... i − 1 1...i-1 1...i1 l y n d o n lyndon lyndon分解且恰好分解完
考虑从 i i i开始,设 j , k j,k j,k两指针,初始 j = i , k = i + 1 j=i,k=i+1 j=i,k=i+1
k k k指向循环节 T T T T ′ TTTT' TTTT的结尾, j = k − ∣ T ∣ j=k-|T| j=kT
s [ j ] = s [ k ] , j + + , k + + s[j]=s[k],j++,k++ s[j]=s[k],j++,k++即可
s [ j ] > s [ k ] s[j]>s[k] s[j]>s[k],那么 k k k之后单独一定更优,而前面是 k − j k-j kj长度的循环节
s [ j ] < s [ k ] , j = i , k + + s[j]s[j]<s[k],j=i,k++重新形成了循环节

复杂度 O ( n ) O(n) O(n)

#include
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair
#define ll long long
#define y1 shinkle
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
inline ll readll(){
    char ch=gc();
    ll res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
inline int readstring(char *s){
	int top=0;char ch=gc();
	while(isspace(ch))ch=gc();
	while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
	s[top+1]='\0';return top;
}
template<typename tp>inline void chemx(tp &a,tp b){a=max(a,b);}
template<typename tp>inline void chemn(tp &a,tp b){a=min(a,b);}
cs int N=(1<<20)|1;
char s[N];
int n;
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	n=readstring(s);
	for(int i=1,j,k;i<=n;){
		j=i,k=i+1;
		while(s[j]<=s[k]){
			if(s[j]==s[k])j++,k++;
			else j=i,k++;
		}
		while(i<=j){
			cout<<i+k-j-1<<" ";
			i+=k-j;
		}
	}
}

你可能感兴趣的:(-----字符串-----)