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 ...
随机推荐
- unix c 02
环境变量 - 存储在内存中的信息,格式是映射,作用就是 帮助系统 进行一些工作,一般是 查找某个东西. 预处理指令:#warning #error #pragma 使用程序直接调用库文件的函数(动态编 ...
- C++关于strcpy等函数的安全版本
如下程序: #include <iostream> using namespace std; int main() { ]; strcpy(ch1,"); } 在VS2012上面 ...
- What’s the difference between an interface and an abstract class in Java?
原文 What’s the difference between an interface and an abstract class in Java? It’s best to start answ ...
- php 对象的一些特性
class person { private $name; private $age = 2; public function __construct($name,$age) { $this-> ...
- 再谈cacheAsBitmap
cacheAsBitmap这个属性很多人都知道,但少有人明白它到底是如何生效的.虽然看名字是转换为位图处理,但用起来的时候感觉却也不过如此.所以,不少人最终选择自己转换Bitmap. 当然,自己转Bi ...
- Ubuntu14.04配置cuda-convnet
转载请注明:http://blog.csdn.net/stdcoutzyx/article/details/39722999 在上一个链接中,我配置了cuda,有强大的GPU,自然不能暴殄天物,让资源 ...
- [转]Laravel 4之表单
Laravel 4之表单 http://dingjiannan.com/2013/laravel-forms/ 创建表单 除了原有的方式创建表单,Laravel提供了一种便捷的方式 <!-- a ...
- VC调试笔记
1.windows-32调试: ①使用map文件根据崩溃地址寻找对应的源代码文件和行号 勾选project->settings->link->General mapfile,对应的P ...
- hdu4506小明系列故事——师兄帮帮忙 (用二进制,大数高速取余)
Problem Description 小明自从告别了ACM/ICPC之后,就開始潜心研究数学问题了,一则能够为接下来的考研做准备,再者能够借此机会帮助一些同学,尤其是美丽的师妹.这不,班里唯一的女生 ...
- 使用Xshell连接Ubuntu
使用Xshell连接Ubuntu Xshell是一个安全终端模拟软件,可以进行远程登录.我使用XShell的主要目的是在Windows环境下登录Linux终端进行编码,非常方便.本文简单介绍下它的使用 ...