1. 程式人生 > >Codeforces Round #526 C - The Fair Nut and String /// 組合遞推

Codeforces Round #526 C - The Fair Nut and String /// 組合遞推

題目大意:

給定原字元序列

找出其中所有子序列滿足

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