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 i1i2, …, im where 1 ≤i1 < i2 < … < im ≤ nai1ai2 … 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 ;
}

最新文章

  1. iOS开发 iOS10推送必看(基础篇)
  2. wordexpress
  3. 查看linux中某个端口是否被占用
  4. 循序渐进Python3(三) -- 1 -- 内置函数
  5. [Xamarin] 啟動拍照並且儲存 (转帖)
  6. 生成二维码 打上自定义logo
  7. linux下svn常用指令
  8. C++专题 - Qt是什么
  9. Qt编程之信号与槽-------unresolved external symbol &quot;public: virtual struct QMetaObject const * __thiscall XX::metaObject(void)const
  10. spring多数据源的配置
  11. C#socket通信1
  12. SpringMVC 基于注解的Controller @RequestMapping @RequestParam..
  13. 【录教程必备】推荐几款屏幕录制工具(可录制GIF)
  14. bzoj4198 荷马史诗 哈夫曼编码
  15. Python 爬取猫眼电影最受期待榜
  16. minitab 输入一串数字
  17. 控制台输出到txt
  18. 2018/04/03 每日一个Linux命令 之 lastb/last
  19. bzoj2733: [HNOI2012]永无乡 启发式合并
  20. 部署puppet master/agent模型

热门文章

  1. 零基础逆向工程27_Win32_01_宽字符_MessageBox_win32调试输出
  2. 在window下, Java调用执行bat脚本
  3. 【ros depthimage_to_laser kinect2】
  4. Yii2 components api/controller
  5. 查看mysql历史命令
  6. office密匙
  7. nrm—源管理工具
  8. U盘小偷——C++实现U盘插入检测和文件扫描拷贝
  9. CSS样式表优化
  10. tableviewcell折叠问题,(类似qq列表展开形式) 多个cell同时展开,OC版 和 Swift