枚举后缀,KMP求每种后缀的最小循环节,更新最优解。
#include <bits/stdc++.h>
#define int long long
using namespace std ;
int a , b ;
char s[10000007];
char str[10000007] ;
int nxt[10000007] ;
int n = 0 ;
signed main(){
//freopen("in","r",stdin);
scanf("%d%d",&a,&b);
scanf("%s",str) ;
int len = strlen(str) ;
for(int i = len - 1 ; i >= 0 ; i--){
if(str[i] == '.')break;
s[++n] = str[i] ;
}
int ans = a * 1 - b * 1 ;
for(int i = 2 , j = 0 ; i <= n ; i++) {
while (j && s[i] != s[j+1]) j = nxt[j] ;
if(s[i] == s[j+1])j++;
nxt[i] = j ;
ans = max(ans , a * i - b * (i - nxt[i])) ;
}
cout << ans << endl ;
return 0 ;
}