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
  1. 3
  2. (){[]}
  3. {([(])}
  4. ))[{}]]
  1. Case #1: 6
  2. Case #2: I think H is wrong!
  3. Case #3: 4

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

  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <set>
  8. #include <vector>
  9. #include <sstream>
  10. #include <queue>
  11. #include <typeinfo>
  12. #include <fstream>
  13. #include <map>
  14. #include <stack>
  15. using namespace std;
  16. #define INF 100000
  17. typedef long long ll;
  18. const int maxn=;
  19. char s[maxn];
  20. bool match(char s1,char s2){
  21. if(s1=='('&&s2==')') return true;
  22. if(s1=='['&&s2==']') return true;
  23. if(s1=='{'&&s2=='}') return true;
  24. return false;
  25. }
  26. int main()
  27. {
  28. int t,times=;
  29. scanf("%d",&t);
  30. while(t--){
  31. memset(s,,sizeof(s));
  32. scanf("%s",s);
  33. stack<char> p;
  34. int len=strlen(s),lens=,tmp=,now=;
  35. for(int i=;i<len;i++){
  36. if(s[i]=='('||s[i]=='{'||s[i]=='[')
  37. p.push(s[i]);
  38. else{
  39. if(!p.empty()) {
  40. char top=p.top();
  41. if(match(top,s[i])){
  42. p.pop();
  43. tmp+=;
  44. if(p.empty()){ //如果之前的都匹配完了,则更新now
  45. now+=tmp;
  46. lens=max(lens,now);
  47. tmp=;
  48. }
  49. }
  50. else{
  51. while(!p.empty()) p.pop(); //匹配不成功,则把之前的都清空。
  52. lens=max(lens,tmp); //之前的有没匹配成功的,所以不用更新now。
  53. tmp=;
  54. now=;
  55. }
  56. }
  57. else{
  58. now+=tmp;
  59. lens=max(lens,now);
  60. tmp=;
  61. now=;
  62. }
  63. }
  64. }
  65. printf("Case #%d: ",times++);
  66. if(lens!=) printf("%d\n",lens);
  67. else printf("I think H is wrong!\n");
  68. }
  69. return ;
  70. }

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. [LOJ 1008] Fibsieve`s Fantabulous Birthday

    A - Fibsieve`s Fantabulous Birthday Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%l ...

  2. ☀【组件】数组 array

    <!doctype html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  3. mysql备份脚本

    [root@AY130828161048465847Z ~]# vi mysqlbak.sh #!/bin/bash USERNAME=rootPASSWORD=mysqlDBNAME=test DA ...

  4. bzoj2946 [Poi2000]公共串(SA,SAM)

    [题意] 多串求LCS.   [思路]   主要是想找一下SAM的优越感 :) velui good 后缀数组划分height需要注意不少细节 <_<,然后不停debug   [代码]   ...

  5. Vijos P1060 盒子

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

  6. POJ1840: Eqs(hash问题)

    一道典型的hash问题: 已知a1,a2,a3,a4,a5,求有多少种不同的<x1,x2,x3,x4,x5>组合满足等式: a1*x1^3 + a2*x2^3 + a3*x3^3 + a4 ...

  7. [读书笔记]了不起的node.js+实践(一)

    环境的变化带来了技术大跃进,机遇和挑战同时到来.基于我js也没有学,只好赶鸭子上架一起学了.(>﹏<) 1.先读读书 一开始就不知死活地看<深入浅出node.js>,弄得团团转 ...

  8. c++学生成绩管理系统

    虽然比较水 =.= 但是写了两节课+一个中午 都是强迫症的锅 http://www.cnblogs.com/wenruo/p/4940182.html #include <cstdio> ...

  9. 开始使用storm

     开始使用storm 本章将讲述如何安装.部署.启动和停止 Storm 集群. Storm 的安装比较简单,但在安装 Storm 之前需要做好充足的准备,本章将介绍安装的整个流程.在官网上可以下载到S ...

  10. javascript函数的4种调用方式

    在javascript中一共有4种函数调用模式,分别是:方法调用模式.函数调用模式.构造函数调用模式和apply(call)调用模式,这4种模式的主要差异在于如何初始化关键参数this. 方法调用模式 ...