题意:求最长的合法括号序列

解:栈+分类讨论

now表示已经算出的序列,且此序列与现在扫描的序列可能能够连接,tmp表示现在扫描到的序列长度

左括号入栈

右括号:1.栈空时:统计当前总长 并且将栈,now,tmp清空

2.栈不空:(1)匹配:tmp+2,弹栈,如果弹栈后栈为空,now=now+tmp相当于把现在算出的和之前算出的连起来,因为此时栈空,已经没有括号挡在两段序列之间

(2)不匹配:now=tmp=0,栈清空

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<string> using namespace std; int T;
char s[];
char stk[]; bool check(char c1,char c2){
if (c1=='(' && c2==')') return true;
if (c1=='{' && c2=='}') return true;
if (c1=='[' && c2==']') return true;
return false;
} int main(){
scanf("%d",&T);
for (int cas=;cas<=T;cas++){
scanf("%s",s);
int len=strlen(s);
int ans=;
int now=;
int tmp=;
int top=;
for (int i=;i<len;i++){
if (s[i]==')' || s[i]=='}' || s[i]==']'){
if (top> && check(stk[top],s[i])){
tmp+=;
top--;
if (top==){
now+=tmp;
tmp=;
ans=max(ans,now);
}
}
else{
if (top> && !check(stk[top],s[i])){
ans=max(ans,tmp);
top=;
now=;
tmp=;
}
else{
if (top==){
now=now+tmp;
ans=max(now+tmp,ans);
now=tmp=;
}
}
}
}
else{
top++;
stk[top]=s[i];
}
}
ans=max(ans,tmp);
if (top==){
ans=max(ans,now+tmp);
}
if (ans==)
printf("Case #%d: I think H is wrong!\n",cas);
else
printf("Case #%d: %d\n",cas,ans);
}
return ;
}
/*
3
(){[]}
{([(])}
))[{}]] 8
[()(()]{}()) 8
[()(()())
([)(()())
()[(()())
()([()())
()(([)())
()(()]())
()(()()])
()(()())]
*/

uestc 10 In Galgame We Trust的更多相关文章

  1. UESTC_In Galgame We Trust CDOJ 10

    As we all know, there are many interesting (H) games in kennethsnow’s computer. But he sets a passwo ...

  2. CDOJ-10(栈的应用)

    In Galgame We Trust Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  3. python连接Greenplum数据库

    配置greenplum客户端认证 配置pg_hba.conf cd /home/gpadmin/gpdbdata/master/gpseg- vim pg_hba.conf 增加 host all g ...

  4. 【转】python测试开发面试题

    出处:http://my.oschina.net/u/1433482/blog/467954?fromerr=WrfxL2Kw 试卷时间 60分钟,请不要在试卷上作答,用A4纸做答题纸作答. 一,中文 ...

  5. CentOS7 源码安装 PostgreSQL 12

    PostgreSQL 12 源码安装 Table of Contents 1. 下载 2. 准备环境 3. 编译安装 4. 设置环境变量 5. 初始化数据库 6. 配置参数文件 6.1. postgr ...

  6. 2015 UESTC Winter Training #10【Northeastern Europe 2009】

    2015 UESTC Winter Training #10 Northeastern Europe 2009 最近集训都不在状态啊,嘛,上午一直在练车,比赛时也是刚吃过午饭,状态不好也难免,下次比赛 ...

  7. 2018.10.25 uestc上天的卿学姐(计数dp)

    传送门 看了DZYODZYODZYO的题解之后发现自己又sbsbsb了啊. 直接dpdpdp是O(2d)O(2^d)O(2d)更新,O(1)O(1)O(1)查询或者O(1)O(1)O(1)更新,O(2 ...

  8. Favorites of top 10 rules for success

    Dec. 31, 2015 Stayed up to last minute of 2015, 12:00am, watching a few of videos about top 10 rules ...

  9. 10 Biggest Business Mistakes That Every Entrepreneur Should Avoid

    原文链接:http://www.huffingtonpost.com/syed-balkhi/10-biggest-business-mista_b_7626978.html When I start ...

随机推荐

  1. PHP获取IP信息

    <?php /** * 获取客户端IP * @param integer $type 返回类型 0:string,1:long * @return string|long */ function ...

  2. 根据群ID和用户Id查询 + string QueryQunByUserIdAndQunId(int userId, int qunId) V1.0

    #region  根据群ID和用户Id查询 + string QueryQunByUserIdAndQunId(int userId, int qunId)  V1.0 /// <summary ...

  3. collection set

    http://blog.csdn.net/humingfiy/article/details/7946408 Collection:List.SetMap:HashMap.HashTable 如何在它 ...

  4. JLINK烧写BIN文件到nand、norflash、SDRAM

    1. 简要说明 JLink的调试功能.烧写Flash的功能都很强大,但是对于S3C2410.S3C2440的Flash操作有些麻烦:烧写Nor Flash时需要设置SDRAM,否则速率很慢:烧写Nan ...

  5. draw lines on ColumnChart

    原文 http://blog.csdn.net/lixuekun820/article/details/5485042 Summary: Adobe 的 Flex Chart提供了很强大的功能,通过简 ...

  6. 发送通知:Notification

    Intent的主要功能是完成一个Activity跳转到其他Activity或者是Service的操作,表示的是一种 操作的意图. PendingIntent表示的是暂时执行的一种意图,是一种在产生某一 ...

  7. css3 翻转和旋转的差别

    我曾经一直以为旋转跟翻转一样,今日自己旋转了好久都发觉跟翻转差一点点,纠结了十几分钟才明确,仅仅能怪自己的立体感太差了. css3中的transform中有旋转,放缩,倾斜,平移的功能,分别相应的属性 ...

  8. Java面试题之jsp相关

    一.jsp有哪些内置对象?作用分别是什么? 分别有什么方法? 本帖隐藏的内容 答:JSP共有以下9个内置的对象: request 用户端请求,此请求会包含来自GET/POST请求的参数 respons ...

  9. T-SQL变量

    T-SQL中变量分为全局变量和局部变量,分别使用@@和@前缀. 全局变量 常用的全局变量有@@VERSION .@@IDENTITY.@@ERROR.@@ROWCOUNT 用法 select @@VE ...

  10. JS前端知识模块大全

    公司前端:小胖提供,表示感谢 1. 基础 HTML, CSS, JS 文档 W3CSCHOOL: http://www.w3schools.com/ MDN: https://developer.mo ...