MUV LUV EXTRA(Kmp找最小循环节)

MUV LUV EXTRA

[Link](Problem - J - Codeforces)

题意

给你a和b和一个小数,让你找 a × 循 环 节 出 现 到 结 尾 的 长 度 + b × 循 环 节 长 度 a\times循环节出现到结尾的长度+b\times循环节长度 a×+b×的最大值是多少?

题解

把小数部分倒过来,等价于找当前这个字符串的循环节是多少。这个可以用kmp中的ne数组解决,长度为len的字符的循环节就是 l e n − n e [ l e n ] len-ne[len] lenne[len]。知道这个性质以后直接求出ne数组,暴力枚举一遍即可。

循环节的证明[link](kmp next函数 kmp的周期问题,深入了解kmp中next的原理 - Because Of You - 博客园 (cnblogs.com))

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <deque> 
#include <sstream>
#define x first
#define y second
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e7 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
LL n, m, k;
int ne[N];
char str1[N], str2[N];
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n >> m;
    cin >> str1;
    int len = strlen(str1);
    int cnt = 0;
    for (int i = len - 1; i >= 0; i -- )    
        if (str1[i] == '.') break;
        else str2[++ cnt] = str1[i];
    LL res = -INF;
    ne[0] = ne[1] = 0;
    for (int i = 2; i <= cnt; i ++ ) {
        int j = ne[i - 1];
        while (j && str2[i] != str2[j + 1]) j = ne[j];
        if (str2[j + 1] == str2[i]) j ++;
        ne[i] = j;
    }
    for (int i = 1; i <= cnt; i ++ ) 
        res = max(res, n * i - m * (i - ne[i]));
    cout << res << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值