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. CSS3 新特性 开放字体格式WOFF

    疑问 上面这是虾米玩意?    \e806 是在自定义字体表中的字体位置.    好嘛 现在问题来了 WOFF里面是什么东西呢? 怎么才能看到? 用这个:FontCreatorPortable     ...

  2. C/C++内存存储问题

    #include <stdio.h> #include "string.h" #include "malloc.h" void Swap(int a ...

  3. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅰ

    许多应用程序都需要处理有序的元素,但不一定要求他们全部有序,或者是不一定要以此就将他们排序.很多情况下我们会手机一些元素,处理当前键值最大的元素,然后再收集更多的元素,再处理当前键值最大的元素.如此这 ...

  4. hdu 5430 Reflect (数学推导题)

    Problem Description We send a light from one point on a mirror material circle,it reflects N times a ...

  5. php 站内搜索 多表 分页

    借鉴了:http://blog.chinaunix.net/uid-20787846-id-3488253.html 这篇文章 ,在此基础上增加了分页功能 <?php /* 关键字 */ $ke ...

  6. redis实现spring-data-redis整合

    java之redis篇(spring-data-redis整合)  博客链接网址:http://www.cnblogs.com/yjmyzz/tag/redis/ redis的知识:官网 1,利用sp ...

  7. Fabricate equation(dfs + 模拟)

    Fabricate equation Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Other ...

  8. 高仿拉手网底部菜单实现FragmentActivity+Fragment+RadioGroup

    先把欢迎页和引导页的代码上传了http://download.csdn.net/detail/u013134391/7183787不要积分的. 底部菜单条实如今4.0曾经都是用tabhost.如今基本 ...

  9. 笔记--cocos2d-x 3.0 环境搭建

    一.下载资源工具 1.下载cocos2d-x 3.0  官网地址:http://www.cocos2d-x.org/filedown/cocos2d-x-3.0-cn 2.下载VS2012 地址网上搜 ...

  10. 用函数式的 Swift 实现图片转字符画的功能

    今天整理 Pocket 中待看的文章,看到这篇<Creating ASCII art in functional Swift>,讲解如何用 Swift 将图片转成 ASCII 字符.具体原 ...