博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1141 Brackets Sequence
阅读量:5942 次
发布时间:2019-06-19

本文共 2497 字,大约阅读时间需要 8 分钟。

Brackets Sequence
Time Limit: 1000MS   Memory Limit: 65536K
        Special Judge

Description

Let us define a regular brackets sequence in the following way:
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

The input file contains at most 100 brackets (characters '(', ')', '[' and ']') that are situated on a single line without any other characters among them.

Output

Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.

Sample Input

([(]

Sample Output

()[()]

Source

 
区间dp记录路径,结合了上两题的括号匹配+回文串修改。。
注意空行
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 const int N = 110; 9 #define For(i,n) for(int i=1;i<=n;i++)10 #define Rep(i,l,r) for(int i=l;i<=r;i++)11 #define Down(i,r,l) for(int i=r;i>=l;i--)12 13 struct State{14 int v,op;15 }dp[N][N];16 17 char st[N];18 int n;19 20 bool match(int x,int y){21 if(st[x]=='(') return (st[y]==')');22 if(st[x]=='[') return (st[y]==']');23 return false;24 }25 void DP(){26 n=strlen(st+1);27 For(i,n)28 For(j,n)29 if(j
dp[i+1][j-1].v){36 dp[i][j].v=dp[i+1][j-1].v;37 dp[i][j].op=0;38 }39 Rep(k,i,j-1)40 if(dp[i][k].v+dp[k+1][j].v
r) return;49 if(l==r){50 if(st[l]=='('||st[l]==')') printf("()");51 if(st[l]==']'||st[l]=='[') printf("[]");52 return;53 }54 if(dp[l][r].op){55 Print(l,dp[l][r].op);56 Print(dp[l][r].op+1,r);57 }58 else{59 printf("%c",st[l]);60 Print(l+1,r-1);61 printf("%c",st[r]);62 }63 }64 65 int main(){66 while(gets(st+1)){67 if(strlen(st+1)){68 DP();69 Print(1,n);70 }71 puts("");72 }73 return 0;74 }

 

转载于:https://www.cnblogs.com/zjdx1998/p/4053677.html

你可能感兴趣的文章
CentOS使用chkconfig增加开机服务提示service xxx does not support chkconfig的问题解决
查看>>
微服务+:服务契约治理
查看>>
save
查看>>
Android DrawLayout + ListView 的使用(一)
查看>>
clear session on close of browser jsp
查看>>
asp.net mvc Post上传文件大小限制 (转载)
查看>>
关于吃掉物理的二次聚合无法实现的需要之旁门左道实现法
查看>>
mysql出现unblock with 'mysqladmin flush-hosts'
查看>>
oracle exp/imp命令详解
查看>>
开发安全的 API 所需要核对的清单
查看>>
Mycat源码中的单例模式
查看>>
WPF Dispatcher介绍
查看>>
fiddler展示serverIP方法
查看>>
C语言中的随意跳转
查看>>
WPF中如何将ListViewItem双击事件绑定到Command
查看>>
《聚散两依依》
查看>>
小tips:你不知道的 npm init
查看>>
Mac笔记本中是用Idea开发工具在Java项目中调用python脚本遇到的环境变量问题解决...
查看>>
Jmeter也能IP欺骗!
查看>>
Rust 阴阳谜题,及纯基于代码的分析与化简
查看>>