牛客寒假第一场 串

题目:

https://ac.nowcoder.com/acm/contest/9981/A

题目大意:

长度不超过n,且包含子序列“us”的、只由小写字母构成的字符串有多少个? 答案对109+710^9+7109+7取模。
所谓子序列,指一个字符串删除部分字符(也可以不删)得到的字符串。
例如,"unoacscc"包含子序列"us",但"scscucu"则不包含子序列"us"
 
动态规划:
设f[n]为当前由n个字母构成含有us的个数
往前n-1个字母串后面加个字母
1.前面已经有us了,最后一位可以加任意的字母
f[i]=f[i-1]*26;
 
2前面只出现u没出现s,最后一位必须加s
前面只出现u没出现s的串有多少个=总数-前面没有u的-前面有us的;
                =26^(i-1)-25^(i-1)-f[i-1]
 
代码:
#include<stdio.h>
const int mod=1e9+7;
const int maxn=1e6+3;
#define ll long long
ll f[maxn],s[maxn];
ll qu(ll a,ll b)
{
   ll ans=1;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%mod;
            a=(a*a)%mod;
            b>>=1;

    }
    return ans%mod;
}
int main()
{
int n;
  scanf("%d",&n);

    f[1]=0;
    f[2]=1;
    for(int i=3;i<=n;i++)
    {
       f[i]=(qu(26,i-1)-qu(25,i-1)+25*f[i-1]+mod)%mod;
    }
        s[0]=0;
    for(int i=1;i<=n;i++)
    {
        s[i]=(s[i-1]%mod+f[i]%mod)%mod;
    }
      printf("%lld\n",s[n]);
}

 

posted @ 2021-07-08 10:44  废柴废柴少女  阅读(103)  评论(0)    收藏  举报