KMP
题意
给定n个数字,令k为该组数组产生循环的下标(即从k~n都循环),p为循环节的长度,要使k+p最小,求(k,p),若有多组求p最小的那组
分析
- 将数字抽象为字符串
令S为原串的最小循环节,则原串的形式应为
X S S S . . . S S S x XSSS...SSS_x XSSS...SSSx
其中 X X X表多余的字符串, S x S_x Sx表 S S S的前缀 - 进一步分析
令 S y S_y Sy为 S S S的后缀
设原串为: X S S S S x XSSSS_x XSSSSx
即: X S x S y S x S y S x S y S x XS_xS_yS_xS_yS_xS_yS_x XSxSySxSySxSySx
求 S S S S x SSSS_x SSSSx的最长公共前后缀next
有 n e x t = S x S y S x S y S x next=S_xS_yS_xS_yS_x next=SxSySxSySx
即 n e x t = S S S x next=SSS_x next=SSSx
而原串 a = S S S S x a=SSSS_x a=SSSSx - 据上述不难得到以下规律
令 a = n ∗ S + S x a=n*S+S_x a=n∗S+Sx
则有 n e x t = ( n − 1 ) ∗ S + S x next=(n-1)*S+S_x next=(n−1)∗S+Sx
因此可得循环节 p = a − n e x t = S p=a-next=S p=a−next=S - 据上述分析用KMP求next数组,进行相应处理即可
Code
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
const int mod=998244353;
const long long inf=1e18;
const int base=131;
const double pi=3.1415926;
#define ll long long
#define int long long
#define ull unsigned long long
#define maxx(a,b) (a>b?a:b)
#define minx(a,b) (a<b?a:b)
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(...) fprintf(stderr, __VA_ARGS__)
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b) { return a*b/gcd(a,b); }
ll inv(ll a) {return a == 1 ? 1 : (ll)(mod - mod / a) * inv(mod % a) % mod;}
ll C(ll n,ll m){if (m>n) return 0;ll ans = 1;for (int i = 1; i <= m; ++i) ans=ans*inv(i)%mod*(n-i+1)%mod;return ans%mod;}
ll A(ll n,ll m){ll sum=1; for(int i=n;i>=n-m+1;i--) sum=(sum*i)%mod; return sum%mod;}
ll GetSum(ll L, ll R) {return (R - L + 1ll) * (L + R) / 2ll;} //等差数列求和
/************/
int n,a[maxn],ne[maxn],ans=inf,ansk,ansp;
void getne(){
for(int i=1,j=0;i<n;i++){
while(a[i]!=a[j]&&j) j=ne[j-1];
if(a[i]==a[j]) j++;
ne[i]=j;
}
}
signed main()
{
IOS;
cin>>n;
for(int i=n-1;i>=0;i--) cin>>a[i];
getne();
for(int i=0;i<n;i++){
int p=i+1-ne[i]; //循环节长度
int tmp=n-1-i+p;
if(ans>=tmp){
if(ans>tmp||(ansp>p)){
ansk=n-1-i;
ansp=p;
}
ans=tmp;
}
}
cout<<ansk<<" "<<ansp;
return 0;
}