poj2955:Brackets
Brackets
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8716 | Accepted: 4660 |
Description
We give the following inductive definition of a “regular brackets” sequence:
- the empty sequence is a regular brackets sequence,
- if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
- if a and b are regular brackets sequences, then ab is a regular brackets sequence.
- no other sequence is a regular brackets sequence
For instance, all of the following character sequences are regular brackets sequences:
(), [], (()), ()[], ()[()]
while the following character sequences are not:
(, ], )(, ([)], ([(]
Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, im where 1 ≤i1 < i2 < … < im ≤ n, ai1ai2 … aim is a regular brackets sequence.
Given the initial sequence ([([]])]
, the longest regular brackets subsequence is [([])]
.
Input
The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (
, )
, [
, and ]
; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.
Output
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.
Sample Input
((()))
()()()
([]])
)[)(
([][][)
end
Sample Output
6
6
4
0
6
分析
dp[l][r]表示区间[l,r]的答案。
状态转移方程,详见代码
- dp[i][j] = max(dp[i+1][j],dp[i][j-1]);
- if ((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
dp[i][j]=dp[i+1][j-1]+2;- dp[l][r]=max(dp[l][r],dp[l][k]+dp[k+1][r]);
code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[];
int dp[][]; int main()
{
while (scanf("%s",s)!=EOF)
{
memset(dp,,sizeof(dp));
if (s[]=='e') break;
int len = strlen(s);
for (int i=len-; i>=; --i)
{
for (int j=i; j<len; ++j)
{
dp[i][j] = max(dp[i+][j],dp[i][j-]);
if ((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
dp[i][j]=dp[i+][j-]+;
for (int k=i; k<j; ++k)
dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+][j]);
}
}
printf("%d\n",dp[][len-]);
}
return ;
}
最新文章
- iOS开发 iOS10推送必看(基础篇)
- wordexpress
- 查看linux中某个端口是否被占用
- 循序渐进Python3(三) -- 1 -- 内置函数
- [Xamarin] 啟動拍照並且儲存 (转帖)
- 生成二维码 打上自定义logo
- linux下svn常用指令
- C++专题 - Qt是什么
- Qt编程之信号与槽-------unresolved external symbol ";public: virtual struct QMetaObject const * __thiscall XX::metaObject(void)const
- spring多数据源的配置
- C#socket通信1
- SpringMVC 基于注解的Controller @RequestMapping @RequestParam..
- 【录教程必备】推荐几款屏幕录制工具(可录制GIF)
- bzoj4198 荷马史诗 哈夫曼编码
- Python 爬取猫眼电影最受期待榜
- minitab 输入一串数字
- 控制台输出到txt
- 2018/04/03 每日一个Linux命令 之 lastb/last
- bzoj2733: [HNOI2012]永无乡 启发式合并
- 部署puppet master/agent模型
热门文章
- 零基础逆向工程27_Win32_01_宽字符_MessageBox_win32调试输出
- 在window下, Java调用执行bat脚本
- 【ros depthimage_to_laser kinect2】
- Yii2 components api/controller
- 查看mysql历史命令
- office密匙
- nrm—源管理工具
- U盘小偷——C++实现U盘插入检测和文件扫描拷贝
- CSS样式表优化
- tableviewcell折叠问题,(类似qq列表展开形式) 多个cell同时展开,OC版 和 Swift