日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 2955 Brackets(括号匹配一)

發布時間:2025/5/22 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 2955 Brackets(括号匹配一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:http://poj.org/problem?id=2955

題目大意:給你一串字符串,求最大的括號匹配數。

解題思路:

設dp[i][j]是[i,j]的最大括號匹配對數。

則得到狀態轉移方程:

if(str[i]=='('&&str[j]==')'||(str[i]=='['&&str[j]==']')){
  dp[i][j]=dp[i+1][j-1]+1;
}
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]) ,(i<=k<j)

代碼:

1 #include<cstdio> 2 #include<cmath> 3 #include<cctype> 4 #include<cstring> 5 #include<iostream> 6 #include<algorithm> 7 #include<vector> 8 #include<queue> 9 #include<set> 10 #include<map> 11 #include<stack> 12 #include<string> 13 #define lc(a) (a<<1) 14 #define rc(a) (a<<1|1) 15 #define MID(a,b) ((a+b)>>1) 16 #define fin(name) freopen(name,"r",stdin) 17 #define fout(name) freopen(name,"w",stdout) 18 #define clr(arr,val) memset(arr,val,sizeof(arr)) 19 #define _for(i,start,end) for(int i=start;i<=end;i++) 20 #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); 21 using namespace std; 22 typedef long long LL; 23 const int N=5e2+5; 24 const int INF=0x3f3f3f3f; 25 const double eps=1e-10; 26 27 int dp[N][N]; 28 char str[N]; 29 30 int main(){ 31 while(~scanf("%s",str)&&strcmp(str,"end")){ 32 int n=strlen(str); 33 memset(dp,0,sizeof(dp)); 34 for(int len=1;len<n;len++){ 35 for(int i=0;i+len<n;i++){ 36 int j=i+len; 37 if(str[i]=='('&&str[j]==')'||(str[i]=='['&&str[j]==']')){ 38 dp[i][j]=dp[i+1][j-1]+1; 39 } 40 for(int k=i;k<j;k++){ 41 dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]); 42 } 43 } 44 } 45 printf("%d\n",dp[0][n-1]*2); 46 } 47 return 0; 48 }

?

轉載于:https://www.cnblogs.com/fu3638/p/8843534.html

總結

以上是生活随笔為你收集整理的POJ 2955 Brackets(括号匹配一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。