标签:file rcm tar name poj inline string har return
括号最大匹配
区间dp
dp[i][j] 表示区间 i~j 的区间最大括号匹配数
考虑dp[i][j]如何转移
对于 dp[i][j] , 可以从 dp[i + 1][j] 转移过来
首先是不匹配的情况
显然 dp[i][j] = dp[i + 1][j]
然后开始在 (i + 1)~j 枚举每一位字符
如果第 k 位的 str[k] 与 str[i] 相匹配 , 那么有
dp[i][j] = max (dp[i][j] , dp[i + 1][k - 1] + dp[k + 1][j] + 2)
代码:
1 #include<cmath> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<iostream> 6 #include<algorithm> 7 #define APART puts("----------------------") 8 #define debug 1 9 #define FILETEST 1 10 #define inf 2010 11 #define ll long long 12 #define ha 998244353 13 #define INF 0x7fffffff 14 #define INF_T 9223372036854775807 15 #define DEBUG printf("%s %d\n",__FUNCTION__,__LINE__) 16 17 namespace chino{ 18 19 const char key[] = "end"; 20 21 inline void setting(){ 22 #if FILETEST 23 freopen("_test.in", "r", stdin); 24 freopen("_test.me.out", "w", stdout); 25 #endif 26 return; 27 } 28 29 inline int read(){ 30 char c = getchar(), up = c; int num = 0; 31 for(; c < ‘0‘ || c > ‘9‘; up = c, c = getchar()); 32 for(; c >= ‘0‘ && c <= ‘9‘; num = (num << 3) + (num << 1) + (c ^ ‘0‘), c = getchar()); 33 return up == ‘-‘ ? -num : num; 34 } 35 36 char s[inf]; 37 int dp[inf][inf]; 38 39 inline bool check(int a, int b){ 40 return s[a] == ‘(‘ ? 41 s[b] == ‘)‘ : 42 s[b] == ‘]‘; 43 } 44 45 inline int main(){ 46 while(true){ 47 scanf("%s", s + 1); 48 if(strcmp(key, s + 1) == 0) 49 break; 50 int n = strlen(s + 1); 51 for(int i = 2; i <= n; i++){ 52 for(int j = i - 1; j; j--){ 53 dp[j][i] = dp[j + 1][i]; 54 if(s[j] == ‘)‘ || s[j] == ‘]‘) 55 continue; 56 for(int k = j + 1; k <= i; k++){ 57 if(check(j, k)) 58 dp[j][i] = std::max (dp[j][i] , dp[j + 1][k - 1] + dp[k + 1][i] + 2); 59 } 60 } 61 } 62 printf("%d\n", dp[1][n]); 63 } 64 return 0; 65 } 66 67 }//namespace chino 68 69 int main(){return chino::main();}
标签:file rcm tar name poj inline string har return
原文地址:https://www.cnblogs.com/chiarochinoful/p/problem-poj-2955.html