區間dp括號匹配
阿新 • • 發佈:2018-10-19
closed lse cin turn amp names strlen pie 區間
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> 12View Code#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 #defineall(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 }View Code
區間dp括號匹配