Strivore(计数,字符串)

该文讨论了一种判断字符串是否为另一字符串子序列的贪心方法,并转化成计算特定长度字符串中满足条件的子序列数量的问题。通过组合数学和模幂运算求解,涉及编程竞赛中的算法设计。
摘要由CSDN通过智能技术生成
//https://vjudge.net/contest/537670#problem/E
#include <bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(), (a).end()
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 2e6 + 10;

/*
对于字符串s,要判断它是否为字符串T的字串的方法:从左到右贪心的去匹配,有一个能匹配就匹配

题解:将问题转化为有多少长度为n+k的字符串使得s是其子序列,
枚举s最后在T中匹配的位置(是贪心的匹配),记作x,对于x之后的位置放什么字母都可以,在x之前选择n-1个位置作为s贪心匹配的位置,那么对于非匹配位置,非匹配位置只要不和下一个匹配位置选择相同的字母即可
*/

ll mod = 1e9 + 7;
ll fac[N], invfac[N];
ll qpow(ll base, ll pow)
{
    ll ans = 1;
    while (pow)
    {
        if (pow & 1)
            ans = ans * base % mod;
        pow >>= 1;
        base = base * base % mod;
    }
    return ans;
}
void init()//不能忘记init和调N的大小
{
     fac[0] = invfac[0] = 1;
     for(int i = 1; i < N; i ++ ) 
         fac[i] = fac[i - 1] * i % mod;
     invfac[N - 1] = qpow(fac[N - 1], mod - 2);
     for(int i = N - 2; i >= 0; i -- ) 
         invfac[i] = invfac[i + 1] * (1 + i) % mod;
}
ll C(int n, int m)
{
    if (n < 0 || m < 0 || m > n)
        return 0;
    return fac[n] * invfac[m] % mod * invfac[n - m] % mod;
}
void solve()
{
    init();
    
    int k;
    cin >> k;

    string s;
    cin >> s;
    int n = s.size();

    ll ans = 0;
    for(int x = n; x <= n + k; x++)
        ans = (ans + C(x - 1, n - 1) * qpow(25, x - n) % mod * qpow(26, n + k - x) % mod) % mod;
    
    cout << ans << '\n';
}

signed main()
{
    IOS;
    int t = 1;
    // cin >> t;
    while (t--)
        solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值