原题传送:http://www.spoj.pl/problems/SQRBR

  动态规划。

  设f[i][j]表示前i个位置在合法情况下缺少j个右括号的方案数。

  转移方程为:

  f[i][j] = f[i-1][j-1] (第i个地方必须为'[')

  f[i][j] = f[i-1][j-1] + f[i-1][j+1] (分第i个位置放左括号和右括号的情况)

  写的第一份代码不是很严谨,j-1变为负值,但spoj判ac了。

 #include <stdio.h>
#include <string.h>
#define N 205 int f[N][N], n, k;
bool h[N]; int main()
{
int t, d;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &k);
memset(h, , sizeof h);
memset(f, , sizeof f);
f[][] = ;
for(int i = ; i <= k; i++)
{
scanf("%d", &d);
h[d] = ;
}
for(int i = ; i <= * n; i++)
{
for(int j = ; j <= * n; j++)
{
if(h[i])
{
f[i][j] = f[i-][j-];
}
else
{
f[i][j] = f[i-][j-] + f[i-][j+];
}
}
}
printf("%d\n", f[*n][]);
}
return ;
}

  修改后为:

 #include <stdio.h>
#include <string.h>
#define N 205 int f[N][N], n, k;
bool h[N]; int main()
{
int t, d;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &k);
memset(h, , sizeof h);
memset(f, , sizeof f);
f[][] = ;
for(int i = ; i <= k; i++)
{
scanf("%d", &d);
h[d] = ;
}
for(int i = ; i <= * n; i++)
{
for(int j = ; j <= * n; j++)
{
if(h[i])
{
if(j != )
f[i][j] = f[i-][j-];
else
f[i][j] = ;
}
else
{
if(j != )
f[i][j] = f[i-][j-] + f[i-][j+];
else
f[i][j] = f[i-][j+];
}
}
}
printf("%d\n", f[*n][]);
}
return ;
}

SPOJ-SQRBR Square Brackets的更多相关文章

  1. Fedora 24中的日志管理

    Introduction Log files are files that contain messages about the system, including the kernel, servi ...

  2. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  3. [LeetCode] Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  4. Markdown语法 中文版

    文章翻译自Markdown创始人JOHN GRUBER的 个人博客, 英文原文请参见 Markdown Syntax; 本文地址: http://www.cnblogs.com/ayning/p/43 ...

  5. iBatis.net 循环iterate,没有foreach

    3.9.4. Iterate Element This tag will iterate over a collection and repeat the body content for each ...

  6. 5种 JavaScript 调用函数的方法

    一次又一次的,我发现,那些有bug的Javascript代码是由于没有真正理解Javascript函数是如何工作而导致的(顺便说一下,许多那样的代码是我写的).JavaScript拥有函数式编程的特性 ...

  7. frp配置

    frps配置 --------------------------------------------------------------------------------------------- ...

  8. sublime text 2 快捷键

    快捷键 功能 ctrl+shift+n 打开新Sublime ctrl+shift+w 关闭Sublime,关闭所有打开文件 ctrl+shift+t 重新打开最近关闭文件 ctrl+n 新建文件 c ...

  9. F#之旅3 - F# PK C#:简单的求和

    原文链接:https://swlaschin.gitbooks.io/fsharpforfunandprofit/content/posts/fvsc-sum-of-squares.html Comp ...

随机推荐

  1. 设置peoplecode trace

    Configuring PeopleCode Trace Select PeopleTools, Utilities, Debug, Trace PeopleCode to access the Tr ...

  2. PHPExcel上传sae遇到: -1:fail to get xml content

    在用PHPExcel1.8.0来处理excel时,本地测试时好使的,但是要把代码部署到SAE,在上传代码的时候就会遇到这个问题. 部署代码中遇到问题: -1:fail to get xml conte ...

  3. Web调用ExE

    把这个文件导入注册表,那么我们在浏览器中输入PrintLabel://1022,那么就会自动调用C:\\Program Files\\xxx有限公司\\Sellercube Label Maker\\ ...

  4. jquery click & get value of attributes of a href

    http://stackoverflow.com/questions/6625667/jquery-click-get-value-of-attributes-of-a-href /* Add a l ...

  5. HTML5高防win8风格

    使用HTML5做了一个高防Win8的页面,大家看看怎么样 Java..NET.PHP.Android.iOS.HTML5,CSS3. web前端.视频资料下载网站  http://51pansou.c ...

  6. 三、MongoDB的创建、更新和删除

    一.MongoDB的下载.安装与部署 二.MongoDB的基础知识简介 三.MongoDB的创建.更新和删除 概要 下面开始学习MongoDB最重要也是最基础的部分:C(创建)R(查询)U(更新)D( ...

  7. The Rotation Game (POJ 2286) 题解

    [问题描述] (由于是英文的,看不懂,这里就把大意给大家说一下吧……都是中国人,相信大家也不愿意看英文……) 如图,一个井字形的棋盘,中间有着1-3任意的数,有ABCDEFGH八个操作,每个操作意味着 ...

  8. 对"使用Mono Runtime Bundle制作安装包让C#桌面应用程序脱离net framework"增加说明

    http://www.cnblogs.com/basilwang/archive/2011/11/29/2267809.html 想做独立引用的估计都看过这一篇文章,但是因为软件更新,很多地方已经不适 ...

  9. Why java main function is declared as static type?

    一个暂且说的过去的解释 The method is static because otherwise there would be ambiguity: which constructor shoul ...

  10. ubuntu12.04和deepin12.06使用root账户登录

    修改lightdm.conf登录方式, 1.首先,激活root账户, 在终端中执行 sudo passwd root 连续两次输入 密码,即使root账户的密码,注意密码并不在终端中显示   2. 切 ...