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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 在ubuntu的bash中循环执行脚本,并在内存不足时重启
#!/bin/bash date ma=`grep MemAvailable /proc/meminfo | awk '{print $2}'` echo MemAvailable = $ma run ...
- SpringMVC,SpringBoot利用ajax上传文件到后台
1.传递单文件 首先html文件中有个<input type=”file” name=”file” id=”file”/>元素. 前台js写法: var formData=new Form ...
- 利用JS模拟排队系统
我爱撸码,撸码使我感到快乐!大家好,我是Counter.今天给大家分享的是js模拟排队系统,刚开始有排队序列,序列里有vip用户和普通用户,vip用户永远位于普通用户的前面,只有当当前vip用户都办理 ...
- Java基础学习-标识符
1.标识符的作用 -给包.类.方法.变量等起名字 2.组成规则 -这里的字符采用的是Unicode字符集,所以包括英文大小写字母,中文字符,数字字符等.不建议使用汉字. -下划 ...
- 数据挖掘领域十大经典算法之—SVM算法(超详细附代码)
https://blog.csdn.net/fuqiuai/article/details/79483057
- JS(JavaScript)的进一步了解7(更新中···)
1.Js操作css样式 div.style.width=”100px”.在div标签内我们添加了一个style属性,并设定 了width值.这种写法会给标签带来大量的style属性,跟实际项目是不符. ...
- pam密码策略
PAM 的使用历史 PAM 是关注如何为服务验证用户的 API.在使用 PAM 之前,诸如 login(和 rlogin.telnet.rsh)之类的应用程序在 /etc/passwd 中查找用户名, ...
- Cordova 笔记
npm instal -g cordova 安装 卸载cordova npm unistall cordova -g 安装指定版本 npm install -g cordova@ 1.创建应用项目及目 ...
- Maven构建 SpringMVC+Spring+MyBatis 环境整合
目录 1. Maven 项目搭建 2. Maven 插件生成 MyBatis 代码 3. 待续 ... 开发环境 开发环境请尽量保持一致,不一致的情况可能存在问题. JDK 1.7 MyEclipse ...
- CART回归树
决策树算法原理(ID3,C4.5) 决策树算法原理(CART分类树) 决策树的剪枝 CART回归树模型表达式: 其中,数据空间被划分为R1~Rm单元,每个单元有一个固定的输出值Cm.这样可以计算模型输 ...