CodeForces - 1101B
题目:
B. Accordion
3 seconds
256 megabytes
standard input
standard output
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 题目大意: 由[::]这样的顺序可以构造手风琴,冒号之间可以有|用来增加手风琴的长度,给出一个串,问串构成的手风琴最长为多少。 思路: 首先,要判断能否构成手风琴,也就是说满足格式[::],不满足就输出-1,满足的话在两个冒号之间数有多少个|,最后加上4就是最长的手风琴长度。要注意,串给出的手风琴可能不止一个,所有要从前向后寻找[,从后往前寻找]。 AC代码如下:
#include<stdio.h>
#include<string.h> int main()
{
char a[500010];
int A=-1,B=-1,C=-1,D=-1,cnt=0;
scanf("%s",a);
int len=strlen(a);
for(int i=0;i<len;i++)
{
if(a[i]=='['){
A=i;break;
}
}
for(int i=len-1;i>A;i--)
{
if(a[i]==']'){
B=i;break;
}
}
for(int i=A+1;i<B;i++)
{
if(a[i]==':'){
C=i;break;
}
}
for(int i=B-1;i>C;i--)
{
if(a[i]==':'){
D=i;break;
}
}
for(int i=C;i<D;i++)
{
if(a[i]=='|') cnt++;
}
if(A==-1||B==-1||C==-1||D==-1) printf("-1\n");
else printf("%d\n",cnt+4);
return 0;
}
CodeForces - 1101B的更多相关文章
- Accordion CodeForces - 1101B (实现)
An accordion is a string (yes, in the real world accordions are musical instruments, but let's forge ...
- 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 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- redis集群部署+节点端口修改+数据恢复
环境:OS:Centos 7Redis: 3.2.11主 从192.168.1.118:7001 192.168.1.118:8001192.168.1.118:7002 192.168.1.118: ...
- NOIP 2018 划水记
(此处不应有目录) (本来想咕掉这篇游记) Day -1 今天信心题,这个毒瘤出题人怎么出了一堆垃圾题(smallfat批判这个垃圾题). T2,T3是送分题.T1考了个noip根本不会考得类欧几里德 ...
- VC++ 实现程序重启
转载:https://blog.csdn.net/what951006/article/details/72729448 一.创建一个Win32项目 二.窗口处理函数中,Create窗口时创建一个按钮 ...
- Android中获得网络状况的实现
要得知网络状况就是要用networkinfo类这个类名还是把这个类的意思表达的很清晰的,network是网络,info是状况.判断是否有网用到了这个类的IsAvailable方法,这个方法返回的是这个 ...
- 微信支付之App支付
项目接入微信支付的准备工作: 注册成为开发者,进行资质认证,这里是需要300元的审核费用的: 在微信商户平台创建应用,提交等待审核(大致需要5-7个工作日): 应用审核通过之后,进入应用,开通微信支付 ...
- poj1676
保存不完整数字可能对应的数字,注意小时<24,分钟小于59. AC代码 #include <stdio.h> #include <vector> using namesp ...
- libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
In Xcode 9 and Swift 4: Print exception stack to know the reason of the exception: Go to show break ...
- radio为什么不能选择。急急急
<div class="control-group"> <label class="control-label" for="&quo ...
- 2019.4.24(js)
1. 取得正数和负数的绝对值 Math.abs(7.25) Math.abs(-7.25) 2.利用JS刷新页面方法 https://www.cnblogs.com/Chen-XiaoJun/p/62 ...
- MySQL存储过程 CASE语句
MySQL存储过程 CASE语句 除了IF语句,MySQL提供了一个替代的条件语句CASE. MySQL CASE语句使代码更加可读和高效. CASE语句有两种形式:简单的搜索CASE语句. 简单C ...