码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #526 C - The Fair Nut and String /// 组合递推

时间:2018-12-11 19:59:47      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:include   序列   opened   int   def   scan   size   long   分享图片   

题目大意:

给定原字符序列

找出其中所有子序列满足

1.序列内字符都为a

2.若有两个以上的字符 则相邻两个字符在原序列中两者之间存在字符b

的数量

 

将整个字符序列用b分开

此时再得到每个b之间a的数量

即 abbgaaba 得到 v[] = { 1 0 2 1 }

 

此时假设到第 i-1 段 已得到在第 i-1 段内的所有方案数为 ans (长度为1、2、3、... 、i-1)

则在第 i 段时 可由前一段的方案数 和 当前段数量 组合得到ans*v[ i ] (长度为2、3、4、... 、i)

此时第 i 段还可以作为长度为1的方案 即ans=ans*v[ i ] + v[ i ]=(ans+1)*v[ i ]

 

那么递推即可得到所有方案数

技术分享图片
#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int N=1e5+5;
const int mod=1e9+7;
char ch[N];
LL v[N];
int main()
{
    while(~scanf("%s",ch)) {
        int len=strlen(ch);
        memset(v,0,sizeof(v));
        v[0]=1LL;
        int i=0, c=1;
        while(i<len) {
            LL m=0LL;
            while(i<len && ch[i]!=b) {
                if(ch[i]==a) m++;
                i++;
            }
            v[c++]=m;
            i++;
        }
        LL ans=0LL;
        for(int j=1;j<=c;j++)
            ans=(ans+(ans+1LL)*v[j]%mod)%mod;
        printf("%I64d\n",ans);
    }

    return 0;
}
View Code

 

Codeforces Round #526 C - The Fair Nut and String /// 组合递推

标签:include   序列   opened   int   def   scan   size   long   分享图片   

原文地址:https://www.cnblogs.com/zquzjx/p/10104289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!