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

解:栈+分类讨论

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. Android 自定义shape圆形按钮

    Shape的属性: solid 描述:内部填充 属性:android:color 填充颜色 size 描述:大小 属性: android:width 宽 android:height 高 gradie ...

  2. AJAX远程跨域获取数据

    //本地获取json文件 $.ajax({ url : 'json.php', type : 'post', dataType : 'json',//返回json数据格式 success : func ...

  3. 安装ngix

    第一步:解压源码包 第二步:./configure -->这个时候会提示缺少PCRE 这个时候要安装yum -y install pcre-devel 第三步:./configure --> ...

  4. Landsat8数据不同波段组合的用途

    2013年2月11日发射的Landsat系列最新卫星Landsat8,携带有OLI陆地成像仪和TIRS热红外传感器,Landsat8的OLI陆地成像仪包括9个波段,OLI包括了ETM+传感器所有的波段 ...

  5. node.js介绍

    官网说明: Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable ...

  6. CxImage的使用

    1.首先从此处下载源代码 http://www.codeproject.com/KB/graphics/cximage.aspx 2.然后将里面的工程全部编译一下,我觉得应该是生成对应的库. 3.然后 ...

  7. 用MFC实现OpenGL编程

    一.OpenGL简介 众所周知,OpenGL原先是Silicon Graphics Incorporated(SGI公司)在他们的图形工作站上开发高质量图像的接口.但最近几年它成为一个非常优秀的开放式 ...

  8. Balls Rearrangement(HDU)

    Problem Description Bob has N balls and A boxes. He numbers the balls from 0 to N-1, and numbers the ...

  9. ubuntu连接无线网

    我的ubuntu是因为没有安装无线网卡驱动,首先查看网卡型号 但是我在Broadcom官网上没有找到BCM43142的驱动. 通过谷歌后发现通过安装bcwl-kernel-source来解决这个问题, ...

  10. U-boot中实现Yaffs2+HwEcc

    经过老手的指点,要实现Yaffs2+HwEcc,重点在于chip->ops.mode由MTD_OOB_RAW到MTD_OOB_AUTO.经过几天的筹备,今天要对其下手了.为了真实展现分析移植过程 ...