Unhappy Hacking II

Source: Internet
Author: User

Tags: dynamic planning

Title Description

Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard have 3 keys on It:the 0 key, the 1 key and the BACKSPACE key.
To begin with, the he is using a plain the text editor with the This keyboard. This editor is always displays one string (possibly empty). Just after the editor was launched, this string is empty. When all keys on the keyboard are pressed, the following changes occur to the string:
The 0 key:a letter 0 would be inserted to the right of the string.
The 1 key:a letter 1 would be inserted to the right of the string.
The backspace key:if the string is empty and nothing happens. Otherwise, the rightmost letter of the string is deleted.
Sig has launched the editor, and pressed these keys N times in total. As a result, the editor displays a string s. Find the number of such ways to press the keys, modulo \ (10^9+7\).

Constraints

1≤n≤5000
1≤|s|≤n
s consists of the letters 0 and 1.
Partial Score
Points'll is awarded for passing the test set satisfying 1≤n≤300.

Input

The input is given from standard input in the following format:
N
S

Output

Print the number of the ways to press the keys N times in total such this editor displays the string s in the end, MoD Ulo 109+7.

Sample input

3
0

Sample output

5

Tips

We'll denote the backspace key by B. The following 5 ways to press the keys would cause the editor to display the string 0 in the end:00b, 01B, 0b0, 1b0, BB0. In the last, nothing would happen when the BACKSPACE key is pressed.

Analysis

With \ (dp[i][j]\) , a \ (i\) operation is performed, and the current string length is \ (j\) . In dp[i][j] This state, we have three choices

    • Can press 1 or 0,\ (dp[i+1][j+1]+=dp[i][j]\)
    • Or press BACKSPACE,\ (Dp[i+1][max (0,j-1)]+=dp[i][j]\);

Because the current bad processing is deduced through dp[i][j], the precursor of using DP[I][J] can be updated before reaching Dp[i][j].

Code

Expand

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespaceStdtypedef Long LongllConst intmaxn=5050;Constll mod=1e9+7;Const intinf=0x3f3f3f3f;CharS[MAXN];intDP[MAXN][MAXN];intInv[maxn];ll Pow (ll x,ll N) {ll ans=1, Base=x; while(n) {if(n&1) Ans=ans*base%mod;        Base=base*base%mod; n>>=1; }returnAns;}intsum[2][MAXN];intMainintargcChar Const*argv[]) {inv[0]=1; inv[1]=pow (2, MOD-2); for(inti =2; i < MAXN; ++i) {inv[i]= (LL) inv[i-1]*inv[1]%mod; }intN,m;//scanf ("%d%d", &n,&m);scanf"%d %s", &n,s+1); M=strlen (S+1); for(inti =0; I <= N; ++i) { for(intj =0; j<=i; ++J) {int&u=dp[i][j];if(i==0&&j==0) u=1;Else if(i==0) u=0;Else if(i==1&&j==0) u=1;if(j) Dp[i+1][j-1]= (dp[i+1][j-1]+u)%mod;ElseDp[i+1][j]= (dp[i+1][j]+u)%mod; Dp[i+1][j+1]= (dp[i+1][j+1]+u*2%mod)%mod; }} printf ("%lld\n", (ll) dp[n][m]*inv[m]%mod);return 0;}

Unhappy Hacking II

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.