区间dp括号匹配

POJ2955

匹配则加一,不需要初始化

 1 //#include<bits/stdc++.h>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<cstring>
 7 #include<map>
 8 #include<set>
 9 #include<queue>
10 #include<bitset>
11 #include<utility>
12 #include<functional>
13 #include<iomanip>
14 #include<sstream>
15 #include<ctime>
16 #include<cassert>
17 #define a first
18 #define b second
19 #define mp make_pair
20 #define pb push_back
21 #define pw(x) (1ll << (x))
22 #define sz(x) ((int)(x).size())
23 #define all(x) (x).begin(),(x).end()
24 #define rep(i,l,r) for(int i=(l);i<(r);i++)
25 #define per(i,r,l) for(int i=(r);i>=(l);i--)
26 #define FOR(i,l,r) for(int i=(l);i<=(r);i++)
27 #define debug1(a)28 cout << #a << " = " << a << endl;
29 #define debug2(a,b)30 cout << #a << " = " << a << endl;31 cout << #b << " = " << b << endl;
32 #define debug3(a,b,c)   cout << #a << " = " << a << endl;33 cout << #b << " = " << b << endl;34 cout << #c << " = " << c << endl;
35 #define debug4(a,b,c,d)36 cout << #a << " = " << a << endl;37 cout << #b << " = " << b << endl;38 cout << #c << " = " << c << endl;39 cout << #d << " = " << d << endl;
40 #define debug5(a,b,c,d,e)41 cout << #a << " = " << a << endl;42 cout << #b << " = " << b << endl;43 cout << #c << " = " << c << endl;44 cout << #d << " = " << d << endl;45 cout << #e << " = " << e << endl;
46 #define eps 1e-9
47 #define PIE acos(-1)
48 #define cl(a,b) memset(a,b,sizeof(a))
49 #define fastio ios::sync_with_stdio(false);cin.tie(0);
50 #define lson l , mid , ls
51 #define rson mid + 1 , r , rs
52 #define ls (rt<<1)
53 #define rs (ls|1)
54 #define INF 0x3f3f3f3f
55 #define lowbit(x) (x&(-x))
56 #define sqr(a) a*a
57 #define ll long long
58 #define vi vector<int>
59 #define pii pair<int, int>
60 using namespace std;
61 //**********************************
62 char s[107];
63 int n;
64 int dp[107][107];
65 //**********************************
66 bool match(int a,int b)
67 {
68     char x=s[a],y=s[b];
69     return x==‘[‘&&y==‘]‘||x==‘(‘&&y==‘)‘;
70 }
71 void solve()
72 {
73     FOR(len,2,n)FOR(l,1,n-len+1){
74         int r=l+len-1;
75         dp[l][r]=dp[l+1][r-1]+match(l,r);
76         rep(k,l,r){
77             dp[l][r]=max(dp[l][r],dp[l][k]+dp[k+1][r]);
78         }
79     }
80     printf("%d\n",dp[1][n]<<1);
81 }
82 //**********************************
83 int main()
84 {
85     while(~scanf("%s",s+1)&&s[1]!=‘e‘){
86         n=strlen(s+1);
87         solve();
88     }
89     return 0;
90 }

CF

不匹配加一,注意初始化边界

 1 //#include<bits/stdc++.h>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<cstring>
 7 #include<map>
 8 #include<set>
 9 #include<queue>
10 #include<bitset>
11 #include<utility>
12 #include<functional>
13 #include<iomanip>
14 #include<sstream>
15 #include<ctime>
16 #include<cassert>
17 #define fi first
18 #define se second
19 #define mp make_pair
20 #define pb push_back
21 #define pw(x) (1ll << (x))
22 #define sz(x) ((int)(x).size())
23 #define all(x) (x).begin(),(x).end()
24 #define rep(i,l,r) for(int i=(l);i<(r);i++)
25 #define per(i,r,l) for(int i=(r);i>=(l);i--)
26 #define FOR(i,l,r) for(int i=(l);i<=(r);i++)
27 #define debug1(a)28 cout << #a << " = " << a << endl
29 #define debug2(a,b)30 cout << #a << " = " << a << endl;31 cout << #b << " = " << b << endl;
32 #define debug3(a,b,c)   cout << #a << " = " << a << endl;33 cout << #b << " = " << b << endl;34 cout << #c << " = " << c << endl;
35 #define debug4(a,b,c,d)36 cout << #a << " = " << a << endl;37 cout << #b << " = " << b << endl;38 cout << #c << " = " << c << endl;39 cout << #d << " = " << d << endl;
40 #define debug5(a,b,c,d,e)41 cout << #a << " = " << a << endl;42 cout << #b << " = " << b << endl;43 cout << #c << " = " << c << endl;44 cout << #d << " = " << d << endl;45 cout << #e << " = " << e << endl
46 #define eps 1e-9
47 #define PIE acos(-1)
48 #define cl(a,b) memset(a,b,sizeof(a))
49 #define fastio ios::sync_with_stdio(false);cin.tie(0);
50 #define lson l , mid , ls
51 #define rson mid + 1 , r , rs
52 #define ls (rt<<1)
53 #define rs (ls|1)
54 #define INF 0x3f3f3f3f
55 #define lowbit(x) (x&(-x))
56 #define sqr(a) a*a
57 #define ll long long
58 #define vi vector<int>
59 #define pii pair<int, int>
60 using namespace std;
61 //**********************************
62 int a[507];
63 int n;
64 int dp[507][507];
65 //**********************************
66 bool match(int x,int y)
67 {
68     return a[x]==a[y];
69 }
70 void solve()
71 {
72     cl(dp,INF);
73     FOR(i,1,n){
74         dp[i][i]=1;
75         if(a[i]==a[i+1])dp[i][i+1]=1;
76     }
77     FOR(len,2,n){
78         FOR(l,1,n-len+1){
79             int r=l+len-1;
80             if(match(l,r)){
81                 if(r==l+1)dp[l][r]=1;
82                 else dp[l][r]=dp[l+1][r-1];
83             }
84 //            if(match(l,r))dp[l][r]=min(dp[l][r],dp[l+1][r-1]);
85             rep(k,l,r){
86                 dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]);
87             }
88         }
89     }
90     printf("%d\n",dp[1][n]);
91 }
92 //**********************************
93 int main()
94 {
95     cin>>n;
96     FOR(i,1,n)cin>>a[i];
97     solve();
98     return 0;
99 }

原文地址:https://www.cnblogs.com/klaycf/p/9818427.html

时间: 2024-11-09 02:46:11

区间dp括号匹配的相关文章

区间dp 括号匹配 nyoj 15

题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=15 括号匹配(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:6 描述 给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来. 如: []是匹配的 ([])[]是匹配的 ((]是不匹配的 ([)]是不匹配的 输入 第一行输入

POJ 2955 Brackets (区间dp 括号匹配)

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3951   Accepted: 2078 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a reg

区间DP——括号匹配

对应NYOJ题目:点击打开链接 括号匹配(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:6 描述 给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来. 如: []是匹配的 ([])[]是匹配的 ((]是不匹配的 ([)]是不匹配的 输入 第一行输入一个正整数N,表示测试数据组数(N<=10) 每组测试数据都只有一行,是一个字符串

POJ 1141-Brackets Sequence(区间dp括号匹配打印路径)

题目地址:POJ 1141 题意:给出一串由'(')'' [ ' ' ] '组成的串,将给出的括号序列以添加最小数目括号的形式进行配对. 思路:dp[i][j]表示当前子序列需要添加的最小字符数,path存储的是所有子问题的解.然后详情看代码解释. #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #in

POJ2955BRACKETS(区间DP括号匹配)

%E7%94%A8UNITY5%E5%BC%80%E5%8F%91%E7%AC%AC%E4%B8%80%E4%B8%AA%E6%89%8B%E6%9C%BA%E6%B8%B8%E6%88%8F%281%29%E5%90%84%E7%A7%8D%E6%8F%92%E4%BB%B6%E7%9A%84%E5%87%86%E5%A4%87 http://mp3.baidu.com/songlist/502238688?54R6m1&pgC5_8fA5=XF4 http://mp3.baidu.com/s

POJ 1141 Brackets Sequence (线性dp 括号匹配 经典题)

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26407   Accepted: 7443   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 re

区间DP [POJ 1141] Brackets Sequence

Brackets Sequence 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, th

[NYIST15]括号匹配(二)(区间dp)

题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=15 经典区间dp,首先枚举区间的大小和该区间的左边界,这时右边界也可计算出来.首先初始化一个匹配,那就是看看这两个括号是否匹配,即: (s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']') ? dp(i,j) = dp(i+1,j-1)+2) : dp(i,j) = 0 接下来枚举i和j中间的所

poj 2955 Brackets 括号匹配 区间dp

题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+2), a[k]与a[j] 匹配,结果一组数据出错 ([]]) 检查的时候发现dp[2][3]==2,对,dp[2][4]=4,错了,简单模拟了一下发现,dp[2][4]=dp[2][1]+dp[2][3]+2==4,错了 此时2与4已经匹配,2与3已经无法再匹配. 故转移方程改为dp[i][j]=