【BZOJ】1152: [CTSC2006]歌唱王国Singleland

本文解析了一道ACM编程竞赛题目,通过动态规划算法解决了一个关于字符串匹配的问题,并提供了完整的C++实现代码。

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

题解

读错题了,是最后留下一个牛人首长歌颂他,和其他人没有关系,t就相当于数据组数

结论题,具体可看
https://www.zhihu.com/question/59895916/answer/196874145

最后一个求导(1 - z)不拆,最后代入1的时候会消掉,就得出了这个结论

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
//#define ivorysi
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define mo 974711
#define RG register
#define MAXN 200005
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;char c = getchar();T f = 1;
    while(c < '0' || c > '9') {
    if(c == '-') f = -1;
    c = getchar();
    }
    while(c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) putchar('-'),x = -x;
    if(x >= 10) out(x / 10);
    putchar('0' + x % 10);
}
const int MOD = 10000;
int fpow(int x,int c) {
    int res = 1,t = x % MOD;
    while(c) {
    if(c & 1) res = res * t % MOD;
    t = t * t % MOD;
    c >>= 1;
    }
    return res;
}
int N,T,M,a[100005],fail[100005];
void Solve() {
    read(N);read(T);
    while(T--) {
    read(M);
    for(int i = 1 ; i <= M ; ++i) read(a[i]);
    fail[1] = 0;
    for(int i = 2 ; i <= M ; ++i) {
        int p = fail[i - 1];
        while(p && a[p + 1] != a[i]) p = fail[p];
        if(a[p + 1] == a[i]) fail[i] = p + 1;
        else fail[i] = 0;
    }
    int p = M,ans = 0;
    while(p) {
        ans += fpow(N,p);
        p = fail[p];
    }
    ans %= MOD;
    printf("%04d\n",ans);
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

转载于:https://www.cnblogs.com/ivorysi/p/9115107.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值