SPOJ-SQRBR Square Brackets
原题传送: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的更多相关文章
- Fedora 24中的日志管理
Introduction Log files are files that contain messages about the system, including the kernel, servi ...
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- Markdown语法 中文版
文章翻译自Markdown创始人JOHN GRUBER的 个人博客, 英文原文请参见 Markdown Syntax; 本文地址: http://www.cnblogs.com/ayning/p/43 ...
- iBatis.net 循环iterate,没有foreach
3.9.4. Iterate Element This tag will iterate over a collection and repeat the body content for each ...
- 5种 JavaScript 调用函数的方法
一次又一次的,我发现,那些有bug的Javascript代码是由于没有真正理解Javascript函数是如何工作而导致的(顺便说一下,许多那样的代码是我写的).JavaScript拥有函数式编程的特性 ...
- frp配置
frps配置 --------------------------------------------------------------------------------------------- ...
- sublime text 2 快捷键
快捷键 功能 ctrl+shift+n 打开新Sublime ctrl+shift+w 关闭Sublime,关闭所有打开文件 ctrl+shift+t 重新打开最近关闭文件 ctrl+n 新建文件 c ...
- F#之旅3 - F# PK C#:简单的求和
原文链接:https://swlaschin.gitbooks.io/fsharpforfunandprofit/content/posts/fvsc-sum-of-squares.html Comp ...
随机推荐
- 史上最全Vim快捷键键位图(入门到进阶)
经典版 下面这个键位图应该是大家最常看见的经典版了. 对应的简体中文版 其实经典版是一系列的入门教程键位图的组合结果,下面是不同编辑模式下的键位图. 入门版 基本操作的入门版. 进阶版 增强版 下图是 ...
- POI中设置Excel单元格格式样式(居中,字体,边框等)
创建sheet什么的就不多说了,直接进入正题 HSSFCellStyle cellStyle = wb.createCellStyle(); 一.设置背景色: cellStyle.setFillF ...
- php数组去重复代码
php数组去重复数据示例,有时候获得的php数组中总是出现value重复的,使用下面的方法就可以去掉重复数据 以数字开头的重复数据如: Array ( [0] => 100 [k1] =&g ...
- Nginx配置:http重定向,URLRewrite,一个简单框架的配置思路
一个重定向的应用配置: server { listen 8000; server_name localhost; root F:/home/projects/test; index ...
- 使用了Theme但是没有效果问题
最近在开发过程中使用了theme移植Preference并使用了一些android样式,但是在自定义的Theme修改了相关参数后却无法实现 可能有些朋友还不知道怎么用.这里也做个简要的使用方式说明. ...
- Laravel 5 基础(十)- 日期,Mutator 和 Scope
在我们前面的解决方案中,直接给 published_at 赋值为当前日期实际上是一个临时解决方案,我们需要设定发布日期,可能是未来2天后才发布,让我们修改这个问题. 首先修改控制器: public f ...
- DevExpress汉化(WinForm)
/* *隔壁老王原创,2013-09-21,转载请保留本人信息及本文地址. *本文地址:http://wallimn.iteye.com/blog/1944191 */ 最简单的方式就是使用汉化资源, ...
- DevExpress 表中数据导出
gridView1.ExportToXlsx("SampleStock.xlsx"); if (true) { DevExpress.XtraEditors.XtraMessage ...
- C#高级功能(三)Action、Func,Tuple
Action和Func泛型委托实际上就是一个.NET Framework预定义的委托,3.5引入的特性.基本涵盖了所有常用的委托,所以一般不用用户重新声明. Action系列泛型委托,是没有返回参数的 ...
- 从客户端检测到有潜在危险的Request.Form 值【转】
asp.net开发中,经常遇到“从客户端检测到有潜在危险的Request.Form 值”错误提示,很多人给出的解决方案是: 1.web.config文档<system.web>后面加入这一 ...