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:
- brackets:
(and) - square brackets:
[and] - 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 |
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 B, AB is also correct.
解题报告:
栈的简单应用题~,从左到右扫一遍,属于合法序列的flag标记打上,之后再扫一次,确认最长的连续合法长度
#include <iostream>
#include <algorithm>
#include <cstring>
const int maxn = + ;
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=;
scanf("%d",&Case);
while(Case--)
{
scanf("%s",s);
int top = , ans = , len = strlen(s), pt = ;
memset(flag,false,sizeof(flag));
for(int i = ; i < len ; ++ i)
{
if (s[i] == '(' || s[i] == '[' || s[i] == '{')
pos[top++] = i;
else if (top > )
{
int tpos = pos[--top];
char ts = s[tpos];
if (match(ts,s[i]))
{
flag[tpos] = true;
flag[i] = true;
}
else
top = ;
}
}
for(int i = ; i < len ; ++ i)
if (!flag[i])
{
ans = max(ans,pt);
pt = ;
}
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 ;
}
UESTC_In Galgame We Trust CDOJ 10的更多相关文章
- uestc 10 In Galgame We Trust
题意:求最长的合法括号序列 解:栈+分类讨论 now表示已经算出的序列,且此序列与现在扫描的序列可能能够连接,tmp表示现在扫描到的序列长度 左括号入栈 右括号:1.栈空时:统计当前总长 并且将栈,n ...
- CDOJ-10(栈的应用)
In Galgame We Trust Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Othe ...
- 在Postman中使用不受信任的SSL证书
阅读目录 第一种方案——临时添加到受信任的证书颁发机构: 第二种方案——永久添加到受信任的证书颁发机构: add by zhj: 在http://www.cnblogs.com/ajianbeyour ...
- 在Postman中使用不受信任的SSL证书(转)
add by zhj: 在http://www.cnblogs.com/ajianbeyourself/p/3898911.html中提到: 对于不受信任的证书,浏览器会发出告警,不过这些也只是告警而 ...
- Juniper SRX防火墙简明配置手册(转)
在执行mit命令前可通过配置模式下show命令查看当前候选配置(Candidate Config),在执行mit后配置模式下可通过run show config命令查看当前有效配置(Active co ...
- 华为HCNP实验 防火墙安全区域及安全策略配置(USG6000)
防火墙安全区域及安全策略配置 一.学习目的 掌握防火墙安全区域的配置方法 掌握安全策略的配置方法 二.拓扑图 三.场景 你是公司的网络管理员.公司总部的网络分成了三个区域,包括 ...
- ScreenOS地址转换
目录 1. NAT-src 1.1 来自DIP池(启用PAT)的NAT-src 1.2 来自DIP池(禁用PAT)的NAT-src 1.3 来自DIP池(带有地址变换)的NAT-src 1.4 来自出 ...
- ScreenOS学习笔记
安全区段 第2层 V1-Trust 同一区段内的接口通信不需要策略,不同区段之间的接口通信则需要策略. Global区段没有接口 V1-Untrust V1-DMZ 第3层 Trust Untrust ...
- pgbouncer的安装和配置
tar -zxvf libevent-2.0.21-stable.tar.gzcd libevent-2.0.21-stable/mkdir /home/pg10/libevent./configur ...
随机推荐
- Linux用户基础
http://itercast.com/lecture/21 操作系统通过用户.组概念来管理使用计算机的人 用户代表一个使用计算机的使用者,操作系统通过用户概念限制一个使用者能够访问的资源 组用来组织 ...
- 什么是野指针?(What is a wild pointer?)
未被初始化的变量称为野指针(wild pointer).顾名思义,我们不知道这个指针指向内存中的什么地址,使用不当程序会产生各种各样的问题. 理解下面的例子: int main() { int *p; ...
- Node.js-require的使用方法
require最常用的方法 require('http') 内置模块 require('./server') “./”表示当前路径,后面跟的是相对路径 require("../lib/se ...
- 前Google人谈团队管理:针对不同员工的情境管理法和如何选择合理的团队规模
本文作者Tomasz Tunguz是Redpoint Ventures的风险投资人,曾在Google担任产品经理并参与过AdSense项目. 我有一个朋友,他创立了一家很成功的公司,而且还在迅速发展. ...
- 学习php常用算法
<?php /*学用php算法*/ /*1.冒泡法 *思路分析:在要排序的一组数中,对当前还未排好的序列, *从前往后对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒. *即,每 ...
- JQuery 补充
筛选: expr String 字符串值,包含供匹配当前元素集合的选择器表达式. jQuery objectobject 现有的jQuery对象,以匹配当前的元素. elem ...
- [R语言画图]气泡图symbols
绘制气泡图主要使用函数symbols(x,y,circle=r).当中x.y是坐标轴,r是每一个点的半径. x<-rnorm(10) y<-rnorm(10) r<-abs(rnor ...
- Ubuntu12.04 Git 服务器详细配置
Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,学过Linux的都知道,Git的优点我就不再多说了,我也是很喜欢Linux的.今天我们一起学习Git服务器在Ubunt ...
- 多个Activity和Intent(转)
根据www.mars-droid.com:Andriod开发视频教学,先跳过书本<Beginning Android 2>的几个章,我是这两个资源一起看,需要进行一下同步.先初步了解一下应 ...
- BackgroundWorker 后台进程控制窗体label、richtextbook内容刷新
昨天写了一个从文章中提取关键词的程序,写完处理的逻辑后又花了好几个小时在用户友好性上.加了几个progressBar,有显示总进度的.有显示分布进度的..因为程序要跑好几个小时才能执行好,只加个总进度 ...