Bzoj2160: 拉拉队排练

本文深入探讨了Manacher算法的实现细节,并通过一个具体的题目示例展示了如何使用该算法来解决问题。文章提供了完整的代码实现,并解释了后缀和后乘法原理的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题面

传送门

Sol

\(Manacher\),开桶记录相同半径的有多少
后缀和后乘法原理

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2e6 + 5);
const int Zsy(19930726);

IL ll Input(){
    RG ll x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, p[_], ans = 1;
ll k, cnt[_];
char s[_];

IL int Pow(RG ll x, RG ll y){
    RG ll ret = 1; x %= Zsy;
    for(; y; y >>= 1, x = x * x % Zsy)
        if(y & 1) ret = ret * x % Zsy;
    return ret;
}

int main(RG int argc, RG char *argv[]){
    n = Input(), k = Input(), scanf(" %s", s + 1);
    for(RG int i = 1, id = 0, mx = 0; i <= n; i++){
        if(i < mx) p[i] = min(mx - i, p[2 * id - i]);
        while(i - p[i] && i + p[i] <= n && s[i - p[i]] == s[i + p[i]]) p[i]++;
        if(i + p[i] > mx) mx = i + p[i], id = i;
    }
    for(RG int i = 1; i <= n; ++i) ++cnt[p[i]];
    for(RG int i = n - 1; i; --i) cnt[i] += cnt[i + 1];
    for(RG int i = n; i; --i){
        if(k >= cnt[i]) k -= cnt[i], ans = 1LL * ans * Pow((i << 1) - 1, cnt[i]) % Zsy;
        else{
            ans = 1LL * ans * Pow((i << 1) - 1, k) % Zsy, k = 0;
            break;
        }
    }
    k ? puts("-1") : printf("%d\n", ans);
    return 0;
}

转载于:https://www.cnblogs.com/cjoieryl/p/8586202.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值