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 ...
随机推荐
- javascript定时器(上)
(一).setInterval 间隔性 function show(){ alert(‘a’); } setInterval(show,1000); 每隔1000毫秒(1秒)执行一次show这个函数: ...
- python-摩尔斯电码查询器
主程序代码:morse_query.py #-*- coding: UTF-8 -*- ' __date__ = '2016/4/5' from Tkinter import * class mors ...
- linux判断日期是否合法
#include <time.h> #include <stdio.h> typedef enum { , } bool; bool check_date(int year, ...
- 算法分析-动态规划(cut_rod)
什么是动态规划,我们要如何描述它? 动态规划算法通常基于一个递推公式及一个或多个初始状态. 当前子问题的解将由上一次子问题的解推出.使用动态规划来解题只需要多项式时间复杂度, 因此它比回溯法.暴力法等 ...
- JavaMail回复
JavaMail邮件回复 http://blog.csdn.net/o_darling/article/details/17558049 http://blog.csdn.net/xiyang_199 ...
- CC EAL认证
国际通用准则(CC) CC(Common Criteria)是国际标准化组织统一现有多种准则的结果,是目前最全面的评价准则.1996年6月,CC第一版发布:1998年5月,CC第二版发布:1999年 ...
- N个数依次入栈,出栈顺序有多少种
题目:N个数依次入栈,出栈顺序有多少种? 首先介绍一下卡特兰数:卡特兰数前几项为 : 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 2 ...
- Qt入门学习——Qt 5 帮助文档的使用
Qt入门学习——Qt 5 帮助文档的使用 学习图形界面开发,肯定离不开帮助文档的使用,因为它不像 C 语言那样就那么几个函数接口,图形接口的接口可以用海量来形容,常用的我们可能能记住,其它的真的没有必 ...
- JQuery(上)
1.流行的JavaScript类库 -- 框架.插件 )为了简化 JavaScript 的开发, 一些 JavsScript 库诞生了. JavaScript 库封装了很多预定义的对象和实用函数 ...
- 自己写一个strcmp函数(C++)
题目说明: 写一个函数,实现两个字符串的比较.即自己写一个strcmp函数,函数原型为int strcmp( char * p1, char * p2); 设p1指向字符串s1,p2指向字符串s2.要 ...