2014 Super training #10 d peanut sequence-DP

Source: Internet
Author: User

Original question: fzu 2170 http://acm.fzu.edu.cn/problem.php? PID = 1, 2170

I did not understand the question at the time, and I didn't even think about the example. So I still feel that it is not easy to do this. Some questions are obviously very simple, but I gave up and even went to play because I didn't understand it. This would not achieve much effect.

Solution:

Definition: DP [I] [J]: The number of labeling schemes in which J of the first I letter belongs to the first sequence.

In case of 'B', the first sequence must be an odd number, that is, J & 1 is transferred. When the number of the second sequence is an odd number (I-j) & 1), it also needs to be transferred because B may belong to the second sequence. Otherwise.

Use a scrolling array to save space.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#define Mod 1000000007using namespace std;#define N 6007int dp[2][N];char ss[N];int main(){    int n,i,j;    int now;    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        scanf("%s",ss);        memset(dp,0,sizeof(dp));        dp[0][0] = 1;        for(i=0,now=1;i<2*n;i++,now=1-now)        {            memset(dp[now],0,sizeof(dp[now]));            if(ss[i] == ‘B‘)            {                for(j=0;j<=n;j++)                {                    if(j&1)                        dp[now][j+1] = (dp[now][j+1]+dp[i&1][j])%Mod;                    if((i-j)&1)                        dp[now][j] = (dp[now][j]+dp[i&1][j])%Mod;                }            }            else            {                for(j=0;j<=n;j++)                {                    if((j&1) == 0)                        dp[now][j+1] = (dp[now][j+1]+dp[i&1][j])%Mod;                    if(((i-j)&1) == 0)                        dp[now][j] = (dp[now][j]+dp[i&1][j])%Mod;                }            }        }        printf("%d\n",dp[0][n]);    }    return 0;}
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.