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:

  1. brackets: ( and )
  2. square brackets: [ and ]
  3. curly brackets: { and }

Kennethsnow’s password must be a correct bracket sequence, and will not be empty.

Zplinti1 found a string written by kennethsnow, and he is sure that kennethsnow’s password is a substring of that, he wonders the maximum possible length of his password, or if his judgment is wrong.

Please note that the original string may also be the password.

Input

The first line of input contains a number T, indicating the number of test cases. (T≤30) For each case, there is a string s, which is the string zplinti1 found. (1≤|s|≤1,000,000, the string will contain those 6 kinds of characters only)

Output

For each case, output Case #i: first. (i is the number of the test case, from 1 to T). If zplinti1’s judgment is wrong (i.e. the answer is 0), output I think H is wrong!, otherwise output a single number, indicating the maximum possible length of kennethsnow’s password.

Sample input and output

Sample Input Sample Output
3
(){[]}
{([(])}
))[{}]]
Case #1: 6
Case #2: I think H is wrong!
Case #3: 4

Hint

We can define a correct bracket sequence more precisely in this way:

Strings ()[], and {} are correct.

For each correct sequence A(A)[A]{A} is also correct.

For each correct sequence A and BAB is also correct.

解题报告:

栈的简单应用题~,从左到右扫一遍,属于合法序列的flag标记打上,之后再扫一次,确认最长的连续合法长度

#include <iostream>
#include <algorithm>
#include <cstring>
const int maxn = + ;
using namespace std; char s[maxn];
int pos[maxn];
bool flag[maxn]; bool match(char s1,char s2)
{
if (s1 == '(' && s2 != ')')
return false;
else if (s1 == '[' && s2 != ']')
return false;
else if (s1 == '{' && s2 != '}')
return false;
return true;
} int main(int argc , char * argv[])
{
int Case,T=;
scanf("%d",&Case);
while(Case--)
{
scanf("%s",s);
int top = , ans = , len = strlen(s), pt = ;
memset(flag,false,sizeof(flag));
for(int i = ; i < len ; ++ i)
{
if (s[i] == '(' || s[i] == '[' || s[i] == '{')
pos[top++] = i;
else if (top > )
{
int tpos = pos[--top];
char ts = s[tpos];
if (match(ts,s[i]))
{
flag[tpos] = true;
flag[i] = true;
}
else
top = ;
}
}
for(int i = ; i < len ; ++ i)
if (!flag[i])
{
ans = max(ans,pt);
pt = ;
}
else
pt++;
ans = max(ans,pt);
printf("Case #%d: ",T++);
if (ans)
printf("%d\n",ans);
else
printf("I think H is wrong!\n");
}
return ;
}

UESTC_In Galgame We Trust CDOJ 10的更多相关文章

  1. uestc 10 In Galgame We Trust

    题意:求最长的合法括号序列 解:栈+分类讨论 now表示已经算出的序列,且此序列与现在扫描的序列可能能够连接,tmp表示现在扫描到的序列长度 左括号入栈 右括号:1.栈空时:统计当前总长 并且将栈,n ...

  2. CDOJ-10(栈的应用)

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

  3. 在Postman中使用不受信任的SSL证书

    阅读目录 第一种方案——临时添加到受信任的证书颁发机构: 第二种方案——永久添加到受信任的证书颁发机构: add by zhj: 在http://www.cnblogs.com/ajianbeyour ...

  4. 在Postman中使用不受信任的SSL证书(转)

    add by zhj: 在http://www.cnblogs.com/ajianbeyourself/p/3898911.html中提到: 对于不受信任的证书,浏览器会发出告警,不过这些也只是告警而 ...

  5. Juniper SRX防火墙简明配置手册(转)

    在执行mit命令前可通过配置模式下show命令查看当前候选配置(Candidate Config),在执行mit后配置模式下可通过run show config命令查看当前有效配置(Active co ...

  6. 华为HCNP实验 防火墙安全区域及安全策略配置(USG6000)

    防火墙安全区域及安全策略配置   一.学习目的 掌握防火墙安全区域的配置方法 掌握安全策略的配置方法   二.拓扑图         三.场景 你是公司的网络管理员.公司总部的网络分成了三个区域,包括 ...

  7. ScreenOS地址转换

    目录 1. NAT-src 1.1 来自DIP池(启用PAT)的NAT-src 1.2 来自DIP池(禁用PAT)的NAT-src 1.3 来自DIP池(带有地址变换)的NAT-src 1.4 来自出 ...

  8. ScreenOS学习笔记

    安全区段 第2层 V1-Trust 同一区段内的接口通信不需要策略,不同区段之间的接口通信则需要策略. Global区段没有接口 V1-Untrust V1-DMZ 第3层 Trust Untrust ...

  9. pgbouncer的安装和配置

    tar -zxvf libevent-2.0.21-stable.tar.gzcd libevent-2.0.21-stable/mkdir /home/pg10/libevent./configur ...

随机推荐

  1. JavaScript实现网页右下角弹出窗口代码

    <script language="JavaScript"><!--var no = 50;var speed = 1;var ns4up = (document ...

  2. July 【补题】

    A(zoj 3596) bfs,记忆搜都可以, 按余数来记录状态. B(zoj 3599) 博弈,跳过 C(zoj 3592) 简单dp,题意不好懂 D(zoj 3602) 子树哈希, 对根的左右儿子 ...

  3. spftlayer 安装及简单使用

    1,yum -y install python-pip; pip(Python packet index);

  4. ssh登录失败处理步骤

    如果登录失败而又找不到显示的原因,优先使用ssh -vT name@ip -p port 进行调试,查看所使用的key文件.ip.端口是否正确.然后再检查下面步骤:1.检查在对应用户名下是否有iden ...

  5. Appstore 创建App步骤

    .进入AppInformation界面 defaultLanguage:选择默认语言 AppName:填写在Appstore上显示的名字 SKUNumber:填写一个唯一标示符,这个只要唯一即可,不能 ...

  6. eclipse里添加类似myeclipse打开当前操作目录

    1.开打eclipse ide,依次run->external tools->external tools configuration 2.在Program下,new一个自己定义的prog ...

  7. [Hapi.js] Replying to Requests

    hapi's reply interface is one of it's most powerful features. It's smart enough to detect and serial ...

  8. The account is locked

    SQL> select * from v$version where rownum=1; BANNER --------------------------------------------- ...

  9. 15. SSH 远程

    一.原理:     使用SSH连接Centos时,我们可以创建一个公钥和一个私钥,公钥放在服务端,私钥放在客户端,当客户端去连接服务端时,会先去查找密钥,     要是客户端的私钥可以和服务端的公钥匹 ...

  10. CSS3 target伪类简介

    CSS3 target伪类是众多实用的CSS3特性中的一个.它用来匹配文档(页面)的URI中某个标志符的目标元素.具体来说,URI中的标志符通常会包含一个”#”字符,然后后面带有一个标志符名称,比如# ...