ZROI#569

ZROI#569

ZROI#569
\(dalao\)们都说这是\(SBDP\),但我就是没想出来,可能是我\(DP\)还不太行叭.\(dalao\)告诉我状态之后我就懂了.
\(f_{i,0/1}\),表示前\(i\)个位置中以\(0/1\)为结尾的交错子序列的个数.
转移就是:
如果当前位置是\(0\):

\[f_{i,0}=f_{i-1,1}+f_{i-1,0} \]

\[f_{i,1}=f_{i-1,1} \]

如果是\(1\)就反过来就行了.
\(Code:\)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = a ; i <= b ; ++ i)
#define per(i,a,b) for (int i = a ; i >= b ; -- i)
#define pii pair < int , int >
#define X first
#define Y second
#define rint read<int>
#define int long long
#define pb push_back

using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;

template < class T >
    inline T read () {
        T x = 0 , f = 1 ; char ch = getchar () ;
        while ( ch < '0' || ch > '9' ) {
            if ( ch == '-' ) f = - 1 ;
            ch = getchar () ;
        }
        while ( ch >= '0' && ch <= '9' ) {
            x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
            ch = getchar () ;
       }
   return f * x ;
}

const int N = 1e6 + 100 ;
const int mod = 1e9 + 7 ;

int n , f[N][2] ;
char s[N] ;
bool v[N] ;

signed main (int argc , char * argv[] ) {
    n = rint () ; scanf ("%s" , s + 1 ) ;
    rep ( i , 1 , n ) v[i] = ( s[i] == 'H' ) ;
    f[0][1] = f[0][0] = 1 ;
    rep ( i , 1 , n ) {
        f[i][v[i]] = ( f[i-1][v[i]] + f[i-1][v[i]^1] ) % mod ;
        f[i][v[i]^1] = f[i-1][v[i]^1] % mod ;
    }
    printf ("%lld\n" , ( f[n][1] + f[n][0] ) % mod - 2 ) ;
    system ("pause") ; return 0 ;
}
posted @ 2019-09-03 10:58  Phecda  阅读(107)  评论(0编辑  收藏  举报

Contact with me