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 ...
随机推荐
- Regarding the %EDIT table
%EDITTABLE is field in the work record DERIVED. This field is generally used a prompt table for vari ...
- js一些稀奇古怪的写法-带你装逼带你飞
//定时器的第三个参数 setInterval(function(str1,str2,num){ alert(str1+str2+num) },1000,'参数1','还可以有很多参数,不同的类型.. ...
- while循环中不支持循环使用curl
<?php $link = mysql_connect('localhost', 'sms', 'sms'); mysql_select_db('sms', $link); mysql_quer ...
- Wordpress模板标签大全
Wordpress模板基本文件 style.css 样式表文件 index.php 主页文件 single.php 日志单页文件 page.php 页面文件 archvie.php 分类和日期存档页文 ...
- android 模拟按键事件
模拟按键事件可以提高代码的复用性,比如在一个edittext的回车事件里做的一些处理 在该edittext的另一个输入要做相同的处理时,模拟按键事件就非常方便了. 代码很简单,直接上代码: new T ...
- python匿名函数(lambda)
简单来说,编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数 当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方 ...
- [笔记]--Python字符串处理
一.截取字符串中的内容 1.截取: MsiExec.exe /I{AC76BA86-7AD7-2052-7B44-AB0000000001} 中的: {AC76BA86-7AD7-2052-7B44- ...
- wpf程序热键的一个类
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServi ...
- Oracle结果集 (MSSQL存储过程写报表)
接触SQL Server比较多,写报表是用存储过程实现. 对Oracle实现像MSSQL那样,还是有很多疑问
- AsyncTask和Handler两种异步方式的实现和区别比较
1 AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以 ...