NOIP 模拟 $92\; \rm 优美的旋律$
题解 \(by\;zj\varphi\)
先枚举起点,然后再枚举循环节长度,再暴力向后一节一节递推。
复杂度时合法的,外层有一个 \(n\),内部复杂度为 \(n+\frac{n}{2}+\frac{n}{3}...+\frac{n}{n}\) 约为 \(n\ln n\),总复杂度为 \(n^2\ln n\)。
Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
namespace IO{
char buf[1<<21],*p1=buf,*p2=buf;
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
#define debug1(x) std::cerr << #x"=" << x << ' '
#define debug2(x) std::cerr << #x"=" << x << std::endl
#define Debug(x) assert(x)
struct nanfeng_stream{
template<typename T>inline nanfeng_stream &operator>>(T &x) {
bool f=false;x=0;char ch=gc();
while(!isdigit(ch)) f|=ch=='-',ch=gc();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
return x=f?-x:x,*this;
}
}cin;
}
using IO::cin;
namespace nanfeng{
#define FI FILE *IN
#define FO FILE *OUT
template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
using ull=unsigned long long;
static const int N=3e3+7,P=13331;
int mx[N],a,b,n;
char s[N];
ull hs[N],p[N],ans;
inline int main() {
FI=freopen("melody.in","r",stdin);
FO=freopen("melody.out","w",stdout);
scanf("%d%d%s",&a,&b,s+1);
n=strlen(s+1);
p[0]=1;
for (ri i(1);i<=n;pd(i)) {
hs[i]=hs[i-1]*P+(ull)(s[i]-'a'+1);
p[i]=p[i-1]*P;
mx[i]=1;
}
for (ri i(1);i<=n;pd(i)) {
const int bs=n-i+1,len=bs>>1;
for (ri j(1);j<=len;pd(j)) {
if (mx[j]>=bs/j) continue;
const ull on=p[j],jud=hs[i+j-1]-hs[i-1]*on;
int nw=bs/j;
for (ri k(2),l;(l=k*j)<=bs;pd(k))
if (hs[i+l-1]-hs[i+l-j-1]*on!=jud) {nw=k-1;break;}
if (mx[j]<nw) ans=cmax(ans,1ull*a*j+1ull*b*nw),mx[j]=nw;
}
}
printf("%llu\n",ans);
return 0;
}
}
int main() {return nanfeng::main();}