POJ 2955 Brackets
来源:互联网 发布:mysql mediumtext 编辑:程序博客网 时间:2024/06/07 02:43
dp[i][j]代表i~j区间内匹配括号的个数,状态转移方程为:
if(str[i]=='('&&str[j]==')'||str[i]=='['&&str[j]==']') dp[i][j]=dp[i+1][j-1]+2;dp[i][j]=max{dp[i][k]+dp[k+1][j]};
代码如下:
#include<iostream>#include<string>#include<algorithm>using namespace std;string str;int dp[105][105];int main() { while (cin >> str, str != "end") { memset(dp, 0, sizeof(dp)); int len = str.length(); for (int k = 1; k < len; ++k) { for (int i = 0, j = k; j < len; ++i, ++j) { if ((str[i] == '('&&str[j] == ')') || (str[i] == '['&&str[j] == ']')) { dp[i][j] = dp[i + 1][j - 1] + 2; } for (int x = i; x < j; ++x) { dp[i][j] = max(dp[i][j], dp[i][x] + dp[x+1][j]); } } } cout << dp[0][len - 1] << endl; } return 0;}
0 0
- DP poj 2955 Brackets
- poj 2955 brackets
- POJ 2955 Brackets
- POJ 2955 Brackets
- POJ 2955 Brackets
- poj 2955 Brackets
- POJ 2955 Brackets
- POJ 2955 Brackets
- poj 2955 Brackets
- POJ 2955 Brackets
- POJ 2955:Brackets
- POJ 2955 Brackets
- POJ 2955 brackets
- POJ 2955 Brackets
- POJ 2955 Brackets
- Brackets (poj 2955)
- poj 2955 Brackets
- POJ 2955 Brackets (基础)
- 1050 To the Max
- libGL error: unable to load driver: radeonsi_dri.so
- 飞鸟集-20160611
- java创建邮件模板
- PeekMessage函数
- POJ 2955 Brackets
- poj2186 popular cows
- 事物的四大特性
- sessionID与cookie
- 前端知识点
- 多线程
- this指针
- [Android]EventBus-来一趟快速公交初体验[上]
- 带参数文件上传