Accordion CodeForces - 1101B (实现)
An accordion is a string (yes, in the real world accordions are musical instruments, but let's forget about it for a while) which can be represented as a concatenation of: an opening bracket (ASCII code 091091), a colon (ASCII code 058058), some (possibly zero) vertical line characters (ASCII code 124124), another colon, and a closing bracket (ASCII code 093093). The length of the accordion is the number of characters in it.
For example, [::], [:||:] and [:|||:] are accordions having length 44, 66 and 77. (:|:), {:||:}, [:], ]:||:[ are not accordions.
You are given a string ss. You want to transform it into an accordion by removing some (possibly zero) characters from it. Note that you may not insert new characters or reorder existing ones. Is it possible to obtain an accordion by removing characters from ss, and if so, what is the maximum possible length of the result?
Input
The only line contains one string ss (1≤|s|≤5000001≤|s|≤500000). It consists of lowercase Latin letters and characters [, ], : and |.
Output
If it is not possible to obtain an accordion by removing some characters from ss, print −1−1. Otherwise print maximum possible length of the resulting accordion.
Examples
|[a:b:|]
4
|]:[|:]
-1
有人说这是一个阅读理解题,觉得有点道理,读懂题后就很简单了,写代码的时候细心一点,情况多多判断就可以了。
题意:给你一个手风琴的字符串模型的定义,然后给你一个字符串,你可以删除这个字符串中的一些或者0个字符,使之成为一个符合定义的字符串,
问这个处理后的字符串最大的长度?
思路:既然题目给定了字符串的定义为[: ||| :] 即左右有开关口的方括号,方括号里面有两个':',':'里面有若干个(可能0个)的'|'字符,
那么只需要处理下左右的方括号后再对里面找对应的字符就好了。
具体细节可以看代码。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[maxn];
int n;
int main()
{
scanf("%s",s);
n=strlen(s);
int spos=-;
for(int i=;i<n;i++)
{
if(s[i]=='[')
{
spos=i;
break;
}
}
int epos=-;
for(int i=n-;i>=;i--)
{
if(s[i]==']')
{
epos=i;
break;
}
} if(epos==-||spos==-||(spos>epos))
{
printf("-1\n");
}else
{
int cnt=;
int ssp=-;
int eep=-;
for(int i=spos;i<=epos;i++)
{
if(s[i]==':')
{
eep=max(eep,i);
if(ssp==-)
{
ssp=i;
}
cnt++;
} }
if(cnt<)
{
printf("-1\n");
}else
{
int ans=;
for(int i=ssp;i<=eep;i++)
{
if(s[i]=='|')
{
ans++;
}
}
printf("%d",ans);
}
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Accordion CodeForces - 1101B (实现)的更多相关文章
- CodeForces - 1101B
题目: B. Accordion time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- Educational Codeforces Round 58 A,B,C,D,E,G
A. Minimum Integer 链接:http://codeforces.com/contest/1101/problem/A 代码: #include<bits/stdc++.h> ...
- Educational Codeforces Round 58 (Rated for Div. 2) 题解
Educational Codeforces Round 58 (Rated for Div. 2) 题目总链接:https://codeforces.com/contest/1101 A. Min ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- mysql性能排查思路
mysql性能瓶颈排查 top/free/vmstat/sar/mpstat 查看mysqld进程的cpu消耗占比 确认mysql进程的cpu消耗是%user, 还是sys%高 确认是否是物理内存 ...
- PE 添加系统管理员账号(域控可加)转
使用U盘制作一个PE系统,这里推荐老毛桃或者大白菜:开机进入Bios,选择U盘启动:进入U盘启动画面后,选择一个PE系统:进入PE系统后,我们去本机系统盘,将 C:/Windows/System32/ ...
- Yii2.0手册地址
官网打不开,可以看这里 http://yii2.techbrood.com/ ;跟官网里面文档一样.ps:今天真郁闷,官网都打不开
- zookeeper-02 部署
1. 主机规划 主机名称 外网IP[外部访问] 内网IP 操作系统 备注 安装软件 docker01 10.0.0.11 172.16.1.11 CentOS 7.2 zookeeper-3.4.5[ ...
- Linux 小知识翻译 - 「如何成为 Linux 内核开发者」
新年的开始,聊聊「怎么做才能成为Linux内核开发者」. Linux内核的开发都是由志愿开发者们完成的.他们并不属于某些特定的企业. 因此,你也有参加Linux内核开发的资格.不用说,卓越的编码技术以 ...
- Unity 琐碎(4) 可视化辅助调试Giamos
Gizmos 类 可以在代码中绘制一些输出结果或者中间输出内容,比如计算后的包围盒等等 类变量 方法 作用 color 控制输出颜色 matrix Set the gizmo matrix used ...
- win32gui.Findwindow(parm1,parm2)查找窗口的句柄方法
介绍之前先让大家了解一下win32gui的模块用法 和 获取窗口类名工具 使用Python时,有时也会要操作到系统窗口的一些东西,下面就介绍win32gui.Findwindow(param1,par ...
- 【Teradata】gtwglobal查看
使用root登录TD数据库节点 cnsterm 6 start gtwglobal cnsterm 3 //上一个命令的window编号 h //帮助 QUIT //退出
- Hadoop下添加节点和删除节点
添加节点 1.修改host 和普通的datanode一样.添加namenode的ip 2.修改namenode的配置文件conf/slaves 添加新增节点的ip或host 3.在新节点的机器 ...
- 【Linux基础】VI命令模式下删除拷贝与粘贴
在VI命令模式下:y 表示拷贝, d 表示删除,p标识粘贴 1.删除 dw 表示删除从当前光标到光标所在单词结尾的内容. d0 表示删除从当前光标到光标所在行首的内容. d$ 表示删除从当前光标到光标 ...