惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
T
Threat Research - Cisco Blogs
N
News and Events Feed by Topic
Hacker News: Ask HN
Hacker News: Ask HN
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 叶小钗
Security Latest
Security Latest
Spread Privacy
Spread Privacy
aimingoo的专栏
aimingoo的专栏
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
U
Unit 42
Cyberwarzone
Cyberwarzone
小众软件
小众软件
Scott Helme
Scott Helme
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
爱范儿
爱范儿
S
Schneier on Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Latest news
Latest news
GbyAI
GbyAI
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
PCI Perspectives
PCI Perspectives
Jina AI
Jina AI
AI
AI
NISL@THU
NISL@THU
I
Intezer
G
GRAHAM CLULEY
B
Blog
S
Secure Thoughts
IT之家
IT之家
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
V2EX - 技术
V2EX - 技术
Recorded Future
Recorded Future
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - Mose

CS学习 NOI2002_ Galaxy银河英雄传说86 [Elite 2008 Dec USACO]Jigsaw Puzzles SGU 223 little kings BSOJ2772 状压DP BZOJ 3132(上帝造题的七分钟-树状数组求和+2D逆求和数组) 埃及分数 [usaco]2013-jan Liars and Truth Tellers 真假奶牛 ubuntu安装php-curl拓展 MIT挑战(如何在12个月内自学完成MIT计算机科学的33门课程|内附MIT公开课程资源和学习顺序 大白话Docker入门(一) Hexo博客搭建全解 代码查重工具sim virtual judge 本地部署方案 POJ题目分类推荐 (很好很有层次感) 解决Ubuntu下Sublime Text 3无法输入中文 [pascal入门]数组 [codecademy]fonts in css [codecademy]css Ubuntu录制gif动态图
【区间DP】codevs3657 括号序列题解
Mose · 2017-05-10 · via 博客园 - Mose

题目描述 Description

我们用以下规则定义一个合法的括号序列:

(1)空序列是合法的

(2)假如S是一个合法的序列,则 (S) 和[S]都是合法的

(3)假如A 和 B 都是合法的,那么AB和BA也是合法的

例如以下是合法的括号序列:

(), [], (()), ([]), ()[], ()[()]

以下是不合法括号序列的:

(, [, ], )(, ([]), ([()

 现在给定一些由'(', ')', '[', ,']'构成的序列 ,请添加尽量少的括号,得到一个合法的括号序列。

输入描述 Input Description

输入包括号序列S。含最多100个字符(四种字符: '(', ')', '[' and ']') ,都放在一行,中间没有其他多余字符。

最后一次把不合法的S变为合法的之前可能情况:
1)S形如(S′)或[S′]:
   只需把S′变合法即可。
   f[i,j]= f[i+1,j-1]
2)S形如(S′ 或[S′:
   先把S′变为合法的,右边加 )或]即可。
   f[i,j]= f[i+1,j]+1

3)S形如   S′)或S′]:
   先把S′化为合法的,左边加(或 [即可。
   f[i,j]= f[i,j-1]+1
4)把长度大于1的序列SiSi+1…..Sj-1Sj分为两部分:
   Si...... Sk,Sk+1….. Sj
   分别化为规则序列.
   则:f[i,j]=f[i,k]+f[k+1,j] ;i<=k<=j-1;
上述4种情况取最小值即可。

代码如下:

 1 //codevs3657 括号序列 区间DP
 2 //copyright by ametake
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 const int maxn=100+10;
 9 const int maxint=0x3f3f3f3f;
10 char s[100+10];
11 int f[maxn][maxn];
12 
13 int main()
14 {
15     scanf("%s",s);
16     int n=strlen(s);
17     for (int i=0;i<n;i++) f[i][i]=1;
18     for (int p=1;p<n;p++)
19     {
20         for (int i=0;i<n-p;i++)
21         {
22             int j=i+p;
23             f[i][j]=maxint;
24             if ((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
25               f[i][j]=min(f[i][j],f[i+1][j-1]);
26             if ((s[i]=='('&&s[j]!=')')||(s[i]=='['&&s[j]!=']'))
27               f[i][j]=min(f[i][j],f[i+1][j]+1);
28             if ((s[i]!='('&&s[j]==')')||(s[i]!='['&&s[j]==']'))
29               f[i][j]=min(f[i][j],f[i][j-1]+1);
30             for (int k=i;k<j;k++) f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);
31         }
32     }
33     printf("%d\n",f[0][n-1]);
34     return 0;
35 }