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 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

分析:括号匹配,把左括号依次入栈,如果碰到右括号,就与栈顶的元素匹配。用now表示当前的长度,tmp表示连续匹配成功的括号长度。具体看代码。

 #include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
using namespace std;
#define INF 100000
typedef long long ll;
const int maxn=;
char s[maxn];
bool match(char s1,char s2){
if(s1=='('&&s2==')') return true;
if(s1=='['&&s2==']') return true;
if(s1=='{'&&s2=='}') return true;
return false;
}
int main()
{
int t,times=;
scanf("%d",&t);
while(t--){
memset(s,,sizeof(s));
scanf("%s",s);
stack<char> p;
int len=strlen(s),lens=,tmp=,now=;
for(int i=;i<len;i++){
if(s[i]=='('||s[i]=='{'||s[i]=='[')
p.push(s[i]);
else{
if(!p.empty()) {
char top=p.top();
if(match(top,s[i])){
p.pop();
tmp+=;
if(p.empty()){ //如果之前的都匹配完了,则更新now
now+=tmp;
lens=max(lens,now);
tmp=;
}
}
else{
while(!p.empty()) p.pop(); //匹配不成功,则把之前的都清空。
lens=max(lens,tmp); //之前的有没匹配成功的,所以不用更新now。
tmp=;
now=;
}
}
else{
now+=tmp;
lens=max(lens,now);
tmp=;
now=;
}
}
}
printf("Case #%d: ",times++);
if(lens!=) printf("%d\n",lens);
else printf("I think H is wrong!\n");
}
return ;
}

CDOJ-10(栈的应用)的更多相关文章

  1. 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 ...

  2. 剑指 offer set 10 栈的压入、弹出序列

    总结 1. 通过按位对比来判断, 没有更优的方法了

  3. C#中堆和栈的区别分析(有待更新总结2)

    转载:http://blog.csdn.net/Zevin/article/details/5731965 线程堆栈:简称栈 Stack 托管堆: 简称堆 Heap 使用.Net框架开发程序的时候,我 ...

  4. 栈的存储结构的实现(C/C++实现)

    存档 #include "iostream.h" #include <stdlib.h> #define max 20 typedef char elemtype; # ...

  5. 栈->栈的应用

    e.g.1 数制转换 十进制数N和其它d进制数的转换是计算机实现计算的基本问题,其解决方法很多,其中一个简单算法基于下列原理. 假设编写一个程序:对于输入的任意一个非负十进制整数,打印输出与其等值的八 ...

  6. php 实现栈结构

    一.栈的定义及知识 1.定义:栈又称为栈或者堆叠,是计算机科学中的一种特殊的串列形式的抽象数据类型,特殊之处在于只允许在链表或者数组的一端(堆栈顶端指针,又称 "top")加入数据 ...

  7. Java的顺序栈和链式栈

    栈的定义 栈是限制在表的一段进行插入和删除的运算的线性表,通常能够将插入.删除的一端为栈顶,例外一端称为栈底,当表中没有任何元素的时候称为空栈. 通常删除(又称"退栈")叫做弹出p ...

  8. C# 堆和栈的区别?

    解释1.栈是编译期间就分配好的内存空间,因此你的代码中必须就栈的大小有明确的定义:堆是程序运行期间动态分配的内存空间,你可以根据程序的运行情况确定要分配的堆内存的大小 解释2. 存放在栈中时要管存储顺 ...

  9. (数组)Largest Rectangle in Histogram(栈解问题)

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  10. 【C#】【数据结构】005-栈:顺序栈

    C#数据结构:顺序栈 1.自定义顺序栈结构: /// <summary> /// 顺序栈 /// </summary> /// <typeparam name=" ...

随机推荐

  1. google官方提供的编译android源码的环境初始化,Initializing a Build Environment

    原文网址:http://source.android.com/source/initializing.html Initializing a Build Environment IN THIS DOC ...

  2. ArcGIS for Android示例解析之高亮要素-----HighlightFeatures

    转自:http://blog.csdn.net/wozaifeiyang0/article/details/7323606 HighlightFeatures 要素高亮化功能,相信有其他gis开发经营 ...

  3. 详解强大的SQL注入工具——SQLMAP

    1. 前言  Windows下的注入工具好的又贵,免费的啊D.明小子等又不好用,我们根本没必要花 时间去找什么破解的havij.pangolin什么的,特别是破解的工具很可能被绑了木马.其实 Linu ...

  4. [NOIP1999]拦截导弹

    1999年NOIP全国联赛提高组 题目描述 Description     某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但 ...

  5. 个性CMD设置方法(转载)

    原文地址http://wenku.baidu.com/link?url=DB8X-eHwE_VGtggBmKsBimdzXeGI_6Ga90W9PmX2Px2eUqdXOnq7FhEIzsqBfTqT ...

  6. linux find用法总结

    前言:我们为什么要学会使用find命令? 转自:http://blog.chinaunix.net/uid-24648486-id-2998767.html 每一种操作系统都有成千上万的文件组成,对于 ...

  7. bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)

    黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...

  8. Vijos P1060 盒子

    Vijos P1060 盒子 链接:https://vijos.org/p/1060 [思路] 组合公式+精度选择. 首先解决将A个数放入N个集合的数目,其中集合可空.因为可以有球不放入集合,所以增加 ...

  9. MTRR内存类型范围寄存器

    1.MTRR的概念 内存类型范围寄存器(MTRRs,翻译过来真别扭,后面都以MTRR直接来说了)提供了一种机制,这种机制其实就是确定在系统内存中物理一段内存的类型.这个类型其实是正对CPU来说的,见图 ...

  10. cocos2d-x 从onEnter、onExit、 引用计数 谈内存泄露问题

    /////////////////////////////////// //author : zhxfl //date   : 2013.8.29 //email  : 291221622@qq.co ...