uestc 10 In Galgame We Trust

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

解:栈+分类讨论

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

左括号入栈

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

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

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

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<queue>
 8 #include<vector>
 9 #include<map>
10 #include<stack>
11 #include<string>
12
13 using namespace std;
14
15 int T;
16 char s[1000007];
17 char stk[1000007];
18
19 bool check(char c1,char c2){
20     if (c1==‘(‘ && c2==‘)‘) return true;
21     if (c1==‘{‘ && c2==‘}‘) return true;
22     if (c1==‘[‘ && c2==‘]‘) return true;
23     return false;
24 }
25
26 int main(){
27     scanf("%d",&T);
28     for (int cas=1;cas<=T;cas++){
29             scanf("%s",s);
30             int len=strlen(s);
31             int ans=0;
32             int now=0;
33             int tmp=0;
34             int top=0;
35             for (int i=0;i<len;i++){
36                     if (s[i]==‘)‘ || s[i]==‘}‘ || s[i]==‘]‘){
37                             if (top>0 && check(stk[top],s[i])){
38                                     tmp+=2;
39                                     top--;
40                                     if (top==0){
41                                             now+=tmp;
42                                             tmp=0;
43                                             ans=max(ans,now);
44                                     }
45                             }
46                             else{
47                                     if (top>0 && !check(stk[top],s[i])){
48                                             ans=max(ans,tmp);
49                                             top=0;
50                                             now=0;
51                                             tmp=0;
52                                     }
53                                     else{
54                                             if (top==0){
55                                                     now=now+tmp;
56                                                     ans=max(now+tmp,ans);
57                                                     now=tmp=0;
58                                             }
59                                     }
60                             }
61                     }
62                     else{
63                             top++;
64                             stk[top]=s[i];
65                     }
66             }
67             ans=max(ans,tmp);
68             if (top==0){
69                     ans=max(ans,now+tmp);
70             }
71             if (ans==0)
72                 printf("Case #%d: I think H is wrong!\n",cas);
73             else
74                 printf("Case #%d: %d\n",cas,ans);
75     }
76     return 0;
77 }
78 /*
79 3
80 (){[]}
81 {([(])}
82 ))[{}]]
83
84 8
85 [()(()]{}())
86
87 8
88 [()(()())
89 ([)(()())
90 ()[(()())
91 ()([()())
92 ()(([)())
93 ()(()]())
94 ()(()()])
95 ()(()())]
96 */
时间: 2024-11-07 02:55:58

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 password for those games. Zplinti1 wants to crack his password and play those games. Kennethsnow uses only 6 kinds of characters to form his password: brack

CDOJ-10(栈的应用)

In Galgame We Trust Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) As we all know, there are many interesting (H) games in kennethsnow’s computer. But he sets a password for those games. Zplinti1 wants to crack hi

双isp小型局域网

说明:R1相当于AC,R2.R3代表两个ISP undo terminal msyssysname S1vlan batch 1 to 8stp bpdu-protectioninter g0/0/1port link-type trunk port trunk allow-pass vlan allbpdu enableinter e0/0/1port link-type accessport default vlan 2stp edged-port enableq -------------

nginx传递lighttpd真实ip

http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModExtForward Module: mod_extforward server.modules  = (        .....        "mod_accesslog",        "mod_extforward"      ) extforward.forwarder = ("10.0.0.232" => &

[从头学数学] 第197节 统计案例

剧情提要: [机器小伟]在[工程师阿伟]的陪同下进入了[九转金丹]之第五转的修炼. 这次要研究的是[统计案例]. 正剧开始: 星历2016年04月26日 16:34:30, 银河系厄尔斯星球中华帝国江南行省. [工程师阿伟]正在和[机器小伟]一起研究[统计案例]. <span style="font-size:18px;">X= [165, 165, 157, 170, 175, 165, 155, 170] Y= [48, 57, 50, 54, 64, 61, 43,

防火墙USG6000V基础配置

实验目的 1掌握防火墙安全区域的配置方法 2掌握安全策略配置方法 实验效果 1 实现防火墙trust到DMZ和untrust的访问 2 实现防火墙tDMZ到untrust的访问 一.防火墙初始配置 USG6600防火墙默认的初始账号密码为,账号admin,密码[email protected],首次登陆时需更改密码.一般情况下,华为新一代的防火墙,默认情况下,只有0口是可以允许所有服务的,用户可根据需求开启相应的端口服务,此处我们是使用模拟器做实验,只需开启在接口模式下,使用命令service-

CentOS7 源码安装 PostgreSQL 12

PostgreSQL 12 源码安装 Table of Contents 1. 下载 2. 准备环境 3. 编译安装 4. 设置环境变量 5. 初始化数据库 6. 配置参数文件 6.1. postgresql.conf 6.2. pg_hba.conf 7. 数据库启动与关闭 7.1. 手动 7.2. 开机自动启动 1 下载 官网提供了源码和预安装的版本. 源码需要编译安装,解决依赖包等问题,而预安装的版本要简单很多.只需下载解压, 初始化数据库即可. 本例以源码安装为例:请至官网 下载源码.

leetcode997 Find the Town Judge

1 """ 2 In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge. 3 If the town judge exists, then: 4 The town judge trusts nobody. 5 Everybody (except for the town judge) trus

百度粉红色风科技上来看积分

http://www.ebay.com/cln/508gua_gvqjq/-/167266747010/2015.02.10 http://www.ebay.com/cln/jhu2290/-/167423283013/2015.02.10 http://www.ebay.com/cln/cha.m22/-/167166250017/2015.02.10 http://www.ebay.com/cln/fenyu56/-/167382503016/2015.02.10 http://www.eb