poj 1141 Brackets Sequence 区间dp,分块记录
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 35049 | Accepted: 10139 | Special Judge | ||
Description
1. Empty sequence is a regular sequence.
2. If S is a regular sequence, then (S) and [S] are both regular sequences.
3. If A and B are regular sequences, then AB is a regular sequence.
For example, all of the following sequences of characters are regular brackets sequences:
(), [], (()), ([]), ()[], ()[()]
And all of the following character sequences are not:
(, [, ), )(, ([)], ([(]
Some sequence of characters '(', ')', '[', and ']' is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n.
Input
Output
Sample Input
([(]
Sample Output
()[()] 题意:用最少的括号,补全答案。 看完题目第一想法可能是,不停地往读入的字符串中插入括号,但这样很难判断哪些是已有的匹配括号。 所以我们可以用一个二维数组pos记录片段,用dp记录区域间最少的的需要补全的括号。 初始化dp[i][i]为1,然后更新dp时顺便更新pos。
然后按pos更新。 注:这题目不知道什么鬼,最后需要输出一个 '\n',否则wa。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int N=;
char s[N];
int dp[N][N],pos[N][N]; bool march(char a,char b)
{
if(a=='('&&b==')'||a=='['&&b==']')
return true;
else
return false;
} void print(int i,int e)
{
if(i>e)
return;
else if(i==e)
{
if(s[i]=='('||s[i]==')')
printf("()");
else if(s[i]=='['||s[i]==']')
printf("[]"); }
else if(pos[i][e]==-)
{
printf("%c",s[i]);
print(i+,e-);
printf("%c",s[e]);
}
else
{
print(i,pos[i][e]);
print(pos[i][e]+,e);
}
} int main()
{
// freopen("input.txt","r",stdin);
gets(s);
// cin>>s;
int len=strlen(s);
memset(dp,,sizeof dp);
memset(pos,,sizeof pos);
for(int i=;i<len;i++)
{
dp[i][i]=;
}
for(int l=;l<len;l++)
{
for(int i=;l+i<len;i++)
{
int e=l+i;
dp[i][e] = 0x7fffffff;
if(march(s[i],s[e]))
{
dp[i][e]=dp[i+][e-];
pos[i][e]=-;
// cout<<"匹配:"<<i<<' '<<e<<' '<<dp[i][e]<<endl;
} for(int j=i;j<e;j++)
{
if(dp[i][e]>dp[i][j]+dp[j+][e])
{
dp[i][e]=dp[i][j]+dp[j+][e];
pos[i][e]=j;
// cout<<i<<' '<<e<<' '<<dp[i][e]<<endl;
}
}
}
}
print(,len-);
printf("\n");
}
poj 1141 Brackets Sequence 区间dp,分块记录的更多相关文章
- POJ 1141 Brackets Sequence(区间DP, DP打印路径)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- poj 1141 Brackets Sequence (区间dp)
题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...
- poj 1141 Brackets Sequence ( 区间dp+输出方案 )
http://blog.csdn.net/cc_again/article/details/10169643 http://blog.csdn.net/lijiecsu/article/details ...
- 区间DP POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29520 Accepted: 840 ...
- POJ 1141 Brackets Sequence (区间DP)
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...
- POJ 1141 Brackets Sequence(括号匹配二)
题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...
- POJ 2955 Brackets (区间dp入门)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29502 Accepted: 840 ...
- Poj 2955 brackets(区间dp)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7795 Accepted: 4136 Descript ...
随机推荐
- 8、Zookeeper分布式锁
基础知识:http://www.cnblogs.com/LiZhiW/p/4931577.html 1 可重入读写锁示例代码如下(lock.acquire加几个,就必须使用几个lock.release ...
- hugo小玩
hugo小玩 1. 安装 install from source by brew install pre-built-binary 2. 下载源码 $ go get github.com/magefi ...
- Skyline TerraExplorer -二次开发- 加载外部数据的各种连接串
Skyline 可以连接外部的数据源,包括SQL Server,Oracle ,excel,mySQL,SQlite,WFS....... 连接字符串如下:例如连接shp文件,为“FileName=C ...
- 学习笔记 urllib
第一步: get # -*- coding:utf-8 -*- # 日期:2018/5/15 19:39 # Author:小鼠标 from urllib import request url = ' ...
- calc()使用用法
calc()是css3的一个新增的功能,用来指定元素的长度. 它是动态设置元素值,可由加减乘除算法得到最后计算值. 比如说“width:calc(50% + 5em)” 在使用less解析中calc运 ...
- oo第一次总结博客
一. 多项式求导问题描述 基本概念的声明: 带符号整数 支持前导 0 的带符号整数,符号可忽略,如:+02.-16.19260817 等. 因子 变量因子 幂函数 一般形式 由自变量x和指数组成,指数 ...
- 关于No qualifying bean of type [XXX.XXX] found for dependency 的一次记录
异常开始于spring+springmvc+mybatis 注解配置,启动tomcat服务器出现No qualifying bean of type [com.***.service] found f ...
- sbt 学习
一.基础 1.工程根目录 包含build.sbt的目录是工程的根目录.注意,就算在一个空目录下面执行sbt about,也会生成project文件夹 2.源文件目录结构 SBT有固定的文件组织结构 s ...
- sql 条件汇总
select * from a pivot(sum([总业绩]) for 周期 in ([1月],[2月],[3月],[4月])) as b
- python2操作MySQL
#coding=utf-8 import MySQLdb conn = MySQLdb.connect(host='localhost',user='root',passwd='123456' ...