zoukankan      html  css  js  c++  java
  • CDOJ-10(栈的应用)

    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. (T30) 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 InputSample Output
    3
    (){[]}
    {([(])}
    ))[{}]]
    Case #1: 6
    Case #2: I think H is wrong!
    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=1000010;
    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=1;
    29     scanf("%d",&t);
    30     while(t--){
    31         memset(s,0,sizeof(s));
    32         scanf("%s",s);
    33         stack<char> p;
    34         int len=strlen(s),lens=0,tmp=0,now=0;
    35         for(int i=0;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+=2;
    44                         if(p.empty()){    //如果之前的都匹配完了,则更新now
    45                             now+=tmp;
    46                             lens=max(lens,now);
    47                             tmp=0;
    48                         }
    49                     }
    50                     else{
    51                         while(!p.empty()) p.pop();  //匹配不成功,则把之前的都清空。
    52                         lens=max(lens,tmp);  //之前的有没匹配成功的,所以不用更新now。
    53                         tmp=0;
    54                         now=0;
    55                     }
    56                 }
    57                 else{
    58                     now+=tmp;
    59                     lens=max(lens,now);
    60                     tmp=0;
    61                     now=0;
    62                 }
    63             }
    64         }
    65         printf("Case #%d: ",times++);
    66         if(lens!=0) printf("%d
    ",lens);
    67         else printf("I think H is wrong!
    ");
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    游戏开发流程梳理
    游戏主策化项目进度管控
    游戏主策化项目开发流程
    设计思想之时间和成长理论
    各类监听时间整理
    Django:学习笔记(1)——开发环境配置
    CNN学习笔记:目标函数
    Keras实践:手写数字识别
    Keras实践:实现非线性回归
    CNN学习笔记:卷积神经网络
  • 原文地址:https://www.cnblogs.com/RRirring/p/4725849.html
Copyright © 2011-2022 走看看