uestc 10 In Galgame We Trust
题意:求最长的合法括号序列
解:栈+分类讨论
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的更多相关文章
- 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 ...
- CDOJ-10(栈的应用)
In Galgame We Trust Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Othe ...
- python连接Greenplum数据库
配置greenplum客户端认证 配置pg_hba.conf cd /home/gpadmin/gpdbdata/master/gpseg- vim pg_hba.conf 增加 host all g ...
- 【转】python测试开发面试题
出处:http://my.oschina.net/u/1433482/blog/467954?fromerr=WrfxL2Kw 试卷时间 60分钟,请不要在试卷上作答,用A4纸做答题纸作答. 一,中文 ...
- CentOS7 源码安装 PostgreSQL 12
PostgreSQL 12 源码安装 Table of Contents 1. 下载 2. 准备环境 3. 编译安装 4. 设置环境变量 5. 初始化数据库 6. 配置参数文件 6.1. postgr ...
- 2015 UESTC Winter Training #10【Northeastern Europe 2009】
2015 UESTC Winter Training #10 Northeastern Europe 2009 最近集训都不在状态啊,嘛,上午一直在练车,比赛时也是刚吃过午饭,状态不好也难免,下次比赛 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Android_自定义进度条
转载:http://blog.csdn.net/lmj623565791/article/details/43371299 ,本文出自:[张鸿洋的博客] 1.概述 最近需要用进度条,秉着不重复造轮子的 ...
- 线程-run和start
import java.lang.Thread; class Machine extends Thread{ public void run() { int a ; for( a = 0 ; a &l ...
- Flink Program Guide (10) -- Savepoints (DataStream API编程指导 -- For Java)
Savepoint 本文翻译自文档Streaming Guide / Savepoints ------------------------------------------------------ ...
- centos6.7配置git服务器
1.yum install -y git 2.adduser git 3.cd /data/git 没有则创建该目录 git init --bare test.git;创建一个裸仓库,没有工作区,不需 ...
- java版括号匹配检测
做一个空栈,读入字符直到结尾.如果读入一个封闭符号,空栈时报错;非空时弹出栈尾字符,如果不匹配则报错.否则读入为开放字符,压入栈中.最后如果栈空,返回true. 其中用到MyStack类,详情请见 p ...
- .Net缓存
近期研究了一下.Net的缓存,据说可以提高系统的性能. .Net缓存分为两种HttpRuntime.Cache和HttpContext.Current.Cache 不过从网上查找资料,说两种缓存其实是 ...
- vs2008 + OpenCV-2.1.0-win32-vs2008安装
vs2008 + OpenCV-2.1.0-win32-vs2008安装 1. 安装vs2008+sp12. 安装opencv-2.1.0-win32-vs2008,假设安装目录为c:/opencv2 ...
- Developer‘s提升开发效率的工具和插件或编程语言
1.Git 之前也有过不少版本控制的工具.有好的,也有糟糕的.不过它们都或多或少地误入歧途了. 这时候Git出现了.一旦你用上了这个神奇的工具,很难相像你还会碰到比它更好的了. 还没用过Git?试一下 ...
- [Leetcode][Python]39: Combination Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...
- Linux学习(一):linux更改ip地址命令_更改DNS_更改默认网关_更改子网掩码_主机名
如何使用命令来更改linux的IP .DNS .子网掩码,在虚拟机(vitrualBox)上添加一个Linux的虚拟机; 安装方法:http://pan.baidu.com/s/1sjJPhP7 安装 ...