zoukankan      html  css  js  c++  java
  • Unhappy Hacking II

    标签 : 动态规划


    题目描述

    Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard only has 3 keys on it: the 0 key, the 1 key and the backspace key.
    To begin with, he is using a plain text editor with this keyboard. This editor always displays one string (possibly empty). Just after the editor is launched, this string is empty. When each key on the keyboard is pressed, the following changes occur to the string:
    The 0 key: a letter 0 will be inserted to the right of the string.
    The 1 key: a letter 1 will be inserted to the right of the string.
    The backspace key: if the string is empty, 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
    400 points will be awarded for passing the test set satisfying 1≤N≤300.

    输入

    The input is given from Standard Input in the following format:
    N
    s

    输出

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

    样例输入

    3
    0

    样例输出

    5

    提示

    We will denote the backspace key by B. The following 5 ways to press the keys will cause the editor to display the string 0 in the end: 00B, 01B, 0B0, 1B0, BB0. In the last way, nothing will happen when the backspace key is pressed.

    分析

    (dp[i][j])来表示,进行了(i)次操作,当前字符串长度为(j)的方法数.在dp[i][j]这个状态下,我们有三种选择

    • 可以按1或0,(dp[i+1][j+1]+=dp[i][j])
    • 或者按退格,(dp[i+1][max(0,j-1)]+=dp[i][j]);

    因为通过dp[i][j]前边的来推导当前不好处理,可以在到达dp[i][j]之前就使用dp[i][j]的前驱更新完毕.

    代码

    expand ```cpp #include #include #include #include using namespace std; typedef long long ll; const int maxn=5050; const ll MOD=1e9+7; const int inf=0x3f3f3f3f; char s[maxn]; int dp[maxn][maxn]; int inv[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; } return ans; } int sum[2][maxn]; int main(int argc, char const *argv[]) { inv[0]=1; inv[1]=Pow(2,MOD-2); for (int i = 2; i < maxn; ++i) { inv[i]=(ll)inv[i-1]*inv[1]%MOD; } int n,m; // scanf("%d%d", &n,&m); scanf("%d %s", &n,s+1); m=strlen(s+1); for (int i = 0; i <= n; ++i) { for (int j = 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; else dp[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 ", (ll)dp[n][m]*inv[m]%MOD); return 0; } ```
  • 相关阅读:
    《淘宝网》为例,描述质量属性的六个常见属性场景
    《苏宁安全架构演进及实践》阅读总结
    《12306核心模型设计思路和架构设计》阅读心得
    放假个人总结七
    放假个人总结六
    放假个人总结五
    连接Oracle提示 ORA-28009: 应当以 SYSDBA 身份或 SYSOPER 身份建立 SYS 连接
    连接oracle无匹配协议
    CentOS 7下虚拟机没有网络
    CentOS 7安装时候没装ifconfig命令
  • 原文地址:https://www.cnblogs.com/sciorz/p/9058582.html
Copyright © 2011-2022 走看看