标签:style blog http color java os io for
([(]
()[()]
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 110; 18 int dp[maxn][maxn],c[maxn][maxn] = {-1}; 19 char str[maxn]; 20 void print(int i,int j) { 21 if(i > j) return; 22 if(i == j) { 23 if(str[i] == ‘(‘ || str[j] == ‘)‘) 24 printf("()"); 25 else printf("[]"); 26 } else { 27 if(c[i][j] >= 0) { 28 print(i,c[i][j]); 29 print(c[i][j]+1,j); 30 } else { 31 if(str[i] == ‘(‘) { 32 printf("("); 33 print(i+1,j-1); 34 printf(")"); 35 } else { 36 printf("["); 37 print(i+1,j-1); 38 printf("]"); 39 } 40 } 41 } 42 } 43 void go() { 44 int len = strlen(str),i,j,k,theMin,t; 45 for(i = 0; i < len; i++) dp[i][i] = 1; 46 for(k = 1; k < len; k++) { 47 for(i = 0; i+k < len; i++) { 48 j = i+k; 49 theMin = dp[i][i]+dp[i+1][j]; 50 c[i][j] = i; 51 for(t = i+1; t < j; t++) { 52 if(dp[i][t]+dp[t+1][j] < theMin) { 53 theMin = dp[i][t]+dp[t+1][j]; 54 c[i][j] = t; 55 } 56 } 57 dp[i][j] = theMin; 58 if(str[i] == ‘(‘ && str[j] == ‘)‘ || str[i] == ‘[‘ && str[j] == ‘]‘) { 59 if(dp[i+1][j-1] < theMin) { 60 dp[i][j] = dp[i+1][j-1]; 61 c[i][j] = -1; 62 } 63 } 64 } 65 } 66 print(0,len-1); 67 } 68 int main() { 69 scanf("%s",str); 70 go(); 71 puts(""); 72 return 0; 73 }
BNUOJ 1260 Brackets Sequence,布布扣,bubuko.com
标签:style blog http color java os io for
原文地址:http://www.cnblogs.com/crackpotisback/p/3915713.html