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. Linux下搭建BT服务器

    P2P(Peer to Peer 即对等网络)就是在这种背景下提出的一种网络技术,P2P可以简单地定义为通过直接交换信息,共享计算机资源和服务,对等计算机兼有客户机和服务器的功能.在这种网络中所有的节 ...

  2. $destroy

    ng-view 路由切换会触发 $destroy

  3. VS2005 工程在win7下使用管理员权限运行

    想关资料 http://stackoverflow.com/questions/13030492/set-administrator-privilege-for-my-c-application-in ...

  4. lua部分 tips

    lua文件刷新 function require_ex( _mname ) if _mname == "" then return end if package.loaded[_m ...

  5. 数据挖掘R与SQL

    本文在Creative Commons许可证下发布 对于数据挖掘专业网站 KDnuggets网站的Poll持保留态度,但它的结果毕竟代表了某一类人群的使用偏好,尤其是在语言角度. 我们看排名前5位的语 ...

  6. Java笔记(十八)……包

    概述 对类文件进行分类管理. 给类提供多层命名空间. 写在程序文件的第一行. 类名的全称的是 包名.类名. 包也是一种封装形式. 访问权限 引用<The Complete Reference&g ...

  7. android获取屏幕分辨率

    DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); dm. ...

  8. makefile for VCS from Syn@psys

    已调试通过 未来将修改成verdi的FSDB版本 下载后把 Makefile_VCS.txt 修改为 Makefile 就可以使用 链接地址: http://files.cnblogs.com/all ...

  9. 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程06:技能播放的逻辑关系》

    6.技能播放的逻辑关系 技能播放概述: 当完成对技能输入与检测之后,程序就该对输入在缓存器中的按键操作与程序读取的技能表信息进行匹配,根据匹配结果播放相应的连招技能. 技能播放原理: 按键缓存器中内容 ...

  10. (原)Struts 相关资源下载

    官网:http://struts.apache.org 点击[Download],进入页面如下,可以看到下载的资源: 点击[struts-2.3.20-all.zip],就能获取Struts2项目所有 ...