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 ...
随机推荐
- shell编程while
脚本编程: 顺序结构 选择结构 if case 循环结构 for while until whil ...
- RedHat安装GCC问题-解决依赖问题
RedHat Linux在安装gcc时需要cpp和cloog-ppl但是在安装cpp的时候需要这个依赖:libmpfr.so.1()(64bit) is needed by cpp-4.4.6-3.e ...
- [转]Hulu 2013北京地区校招笔试题
填空题: 1.中序遍历二叉树,结果为ABCDEFGH,后序遍历结果为ABEDCHGF,逆序遍历结果为? 2.对字符串HELL0_HULU中的字符进行二进制编码,使得字符串的编码长度尽可能短,最短长度为 ...
- 【转】多核CPU运行模式
多核CPU运行模式主要有以下三种: •非对称多处理(Asymmetric multiprocessing,AMP)——每个CPU内核运行一个独立的操作系统或同一操作系统的独立实例(instantiat ...
- JQuery 动画之 广告
html页面: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head&g ...
- UITextField输入长度限制
[_yourTextField addTarget:self action:@selector(eventEditingChange:) forControlEvents:UIControlEvent ...
- JSTL解析——003——core标签库02
上一节主要讲解了<c:if/><c:choose/><c:when/><c:otherwise><c:out/>标签的使用,下面继续讲解其它 ...
- [CSS3] CSS Display Property: Block, Inline-Block, and Inline
Understanding the most common CSS display types of block, inline-block, and inline will allow you to ...
- Oracle安装基本步骤
安装数据库 .建立用户组及用户 groupadd oinstall groupadd dba groupadd oper useradd -g oinstall -G dba oracle passw ...
- zabbix-web界面图形中文乱码解决方法
1.搜索windows-server X86-64机器上C:/windows/fonts的simkai.tts文件,windows7下有时候不行. 2.把它拷贝到zabbix的web端的fonts目录 ...