HDU 1141---Brackets Sequence(区间DP)
2018-06-17 23:49:06来源:未知 阅读 ()
题目链接
http://poj.org/problem?id=1141
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
()[()]
Source
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> using namespace std; const int inf=0x3f3f3f3f; char s[105]; int v[105][105]; int dp[105][105]; void print(int l,int r) { if(r<l) return; if(l==r) { if(s[l]=='('||s[l]==')') printf("()"); else printf("[]"); return; } if(v[l][r]==-1) { if(s[l]=='(') { printf("("); print(l+1,r-1); printf(")"); } else { printf("["); print(l+1,r-1); printf("]"); } } else { print(l,v[l][r]); print(v[l][r]+1,r); } } int main() { scanf("%s",s); int len=strlen(s); memset(dp,0,sizeof(dp)); for(int i=0; i<len; i++) dp[i][i]=2; for(int l=1; l<len; l++) { for(int i=0; i+l<len; i++) { dp[i][i+l]=inf; for(int k=i; k<i+l; k++) { if(dp[i][i+l]>dp[i][k]+dp[k+1][i+l]) { dp[i][i+l]=dp[i][k]+dp[k+1][i+l]; v[i][i+l]=k; } } if(s[i]=='('&&s[i+l]==')'||s[i]=='['&&s[i+l]==']') { if(dp[i][i+l]>dp[i+1][i+l-1]+2) { dp[i][i+l]=dp[i+1][i+l-1]+2; v[i][i+l]=-1; } } } } print(0,len-1); printf("\n"); return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:高精度模板
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- Longest Ordered Subsequence 2020-02-09
- common subsequence 2020-02-09
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash