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 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 = 1000000 + 50;
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=1;
  scanf("%d",&Case);
  while(Case--)
   {
         scanf("%s",s);
         int top = 0 , ans = 0 , len = strlen(s), pt = 0;
         memset(flag,false,sizeof(flag));
         for(int i = 0 ; i < len ; ++ i)
          {
                if (s[i] == ‘(‘ || s[i] == ‘[‘ || s[i] == ‘{‘)
                 pos[top++] = i;
                else if (top > 0)
                 {
                       int  tpos = pos[--top];
                       char ts = s[tpos];
                       if (match(ts,s[i]))
                         {
                             flag[tpos] = true;
                             flag[i] = true;
                }
              else
                top = 0;
           }
       }
      for(int i = 0 ; i < len ; ++ i)
       if (!flag[i])
        {
          ans = max(ans,pt);
          pt = 0;
        }
       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 0;
}
时间: 2024-08-08 01:26:22

UESTC_In Galgame We Trust CDOJ 10的相关文章

uestc 10 In Galgame We Trust

题意:求最长的合法括号序列 解:栈+分类讨论 now表示已经算出的序列,且此序列与现在扫描的序列可能能够连接,tmp表示现在扫描到的序列长度 左括号入栈 右括号:1.栈空时:统计当前总长 并且将栈,now,tmp清空 2.栈不空:(1)匹配:tmp+2,弹栈,如果弹栈后栈为空,now=now+tmp相当于把现在算出的和之前算出的连起来,因为此时栈空,已经没有括号挡在两段序列之间 (2)不匹配:now=tmp=0,栈清空 1 #include<cstdio> 2 #include<iost

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 hi

window7_64安装STAF

1.       安装包下载 从http://sourceforge.net/projects/staf/files/staf/V3.4.17/下载所需安装包,有Windows.Linux.Solaris.Mac等不同平台不同的版本号的安装包,我下载的是大小为109.1MB的: STAF3418Beta1-setup-winamd64.exe,这里有些疑惑的是为什么后缀有winamd的字眼,由于我查看了我的电脑的属性中显示用的是Intel的芯片? 只是事实证明 可用是真的. 2.       W

Juniper NetScreen 基于源NAT转换

1.NAT-Src with PAT Enabled CLI: set int eth1 zone trust set int eth1 ip 10.1.1.1/24 set int eth1 nat set int eth3 zone untrust set int eth3 ip 1.1.1.1/24 set int eth3 route set int eth3 dip 5 1.1.1.30 1.1.1.30 set policy from trust to untrust any any

ScreenOS地址转换

目录 1. NAT-src 1.1 来自DIP池(启用PAT)的NAT-src 1.2 来自DIP池(禁用PAT)的NAT-src 1.3 来自DIP池(带有地址变换)的NAT-src 1.4 来自出口接口IP地址的NAT-src 2. NAT-dst 2.1 一对一目标地址转换 2.2 一对多目标地址转换 2.3 多对一目标地址转换 2.4 多对多目标地址转换 2.5 带有端口映射的NAT-dst 3. MIP 3.1 Untrust区段接口上的MIP 3.2 从不同区段到达MIP 4. VI

ScreenOS学习笔记

安全区段 第2层 V1-Trust 同一区段内的接口通信不需要策略,不同区段之间的接口通信则需要策略. Global区段没有接口 V1-Untrust V1-DMZ 第3层 Trust Untrust DMZ 全局 Global Tunnel区段 Untrust-Tun 功能区段 Null,Self,MGT,HA,VLAN 安全区段是一个或多个网段组成的集合,是绑定了一个或多个接口的逻辑实体. Set zone name zone         //创建名为zone的区段 Set zone z

IPSec VPN不通时的故障处理方法

IPSec VPN不通 介绍了IPSec VPN不通时的故障处理方法. 现象描述 如图1所示,管理员希望在NGFW_A和NGFW_B之间建立IKE方式的IPSec隧道,使网络A和网络B的用户可以通过IPSec隧道互相访问. 图1 IPSec VPN不通组网图  配置完成后,发现网络A和网络B的用户不能相互访问. 可能原因 流量未匹配ACL规则 两端设备的IKE安全提议配置不一致 两端设备的IKE版本不同 对端IP地址或对端域名配置错误 两端设备的预共享密钥配置不一致 未启用NAT穿越功能 两端设

Juniper-SSG系列之子接口(单臂路由)运用

先上图: 三.分析与预规划 规划如上图↑ 分析客户目前暂定的拓扑方案,实现多vlan间通信.G0/0/48端口做成Trunk,理论上SW-A默认只会让10.10.0.X/24的主机过,Juniper防火墙Ping vlanif1-6都能到,这个是问题来了,只有10.10.0.x/24的主机,端口不做情况下就能到Juniper设备上.这时就能意识到,单臂路由的方向!!(*^__^*) [单臂路由定义扫盲] 单臂路由(router-on-a-stick)是指在路由器的一个接口上通过配置子接口(或"逻

Leetcode-997 Find the Town Judge(找到小镇的法官)

作者水平有限,所发仅为个人愚见,如有明显谬误,望斧正 题目可转化为对于所给正整数N(1≤N≤1000),共有N个节点,编号从1-N.其中"相信"这一概念,可看作是一条连接两节点的有向边.如所给二维vector的trust向量数组,trust[i][0]表示有向边的起点,则trust[i][1]表示有向边的终点.所求为满足以下2个条件的节点:①出度(即题目中的属性1)为0 ②入度(即题目中的属性2)为N-1.当且仅当满足以上两个条件的节点数量为1时,所求问题有解,返回节点编号.当满足以上