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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 14: linux实用命令
1.1 基本实用命令整理 1.查找大文件文件 du -sh ./*|grep G # 查看当前目录下个文件大于1G的文件夹 2.查找日志文件中 5xx数量,并进行排序 ...
- 01: 腾讯云API-云服务器
1.1 云服务器 1.腾讯云SDK使用举例 网址:https://cloud.tencent.com/document/sdk/Python #! /usr/bin/env python # -*- ...
- 王之泰 201771010131《面向对象程序设计(java)》第十六周学习总结
第一部分:理论知识学习部分 第14章 并发 ⚫ 线程的概念⚫ 中断线程⚫ 线程状态⚫ 多线程调度⚫ 线程同步 1.程序与进程的概念 1.1程序是一段静态的代码,它是应用程序执行的蓝 本. 1. ...
- EF中防止sql注入
EF作为一个orm框架,本身以及放置了sql的注入,但是如果我们需要执行sql语句的时候了?比如,我们需要查询视图"select * from VM where 条件 = {0}" ...
- 阿里云centos怎么用xshell5登陆
第一种是用ssh,安装Xshell5 打开XShell 新建会话输入ip 选择新建的会话,点击连接,选择接受并保护,输入root,点击确定 输入密码 已经连接成功了,用Xshell ...
- 工作中常用的 Linux 命令
awk 示例: env变量值如下,需要获得pkg_url的链接值: {"name": "michael", "sex": "mal ...
- j2ee课程设计—基于activiti的请休假系统
前言 课设基于SSM框架,数据库采用mysql,主要业务交给activiti,版本控制利用github. 参考资料: Intellij 部署SSM框架 Activiti就是这么简单 方大师的教材 下文 ...
- 趋势:flex和grid使布局更简单
前言:记不久前面试的时候,面试官问我平时用什么布局方式,我非常耿直的说 div+css,利用position,float等布局,这就是非常传统的布局方式,通常都要写比较多的css代码:前几天在知乎上看 ...
- C#socket编程之实现一个简单的TCP通信
TCP(TransmissionControl Protocol)传输控制协议. 是一种可靠的.面向连接的协议(eg:打电话).传输效率低全双工通信(发送缓存&接收缓存).面向字节流.使用TC ...
- 爬虫学习笔记(1)-- 利用Python从网页抓取数据
最近想从一个网站上下载资源,懒得一个个的点击下载了,想写一个爬虫把程序全部下载下来,在这里做一个简单的记录 Python的基础语法在这里就不多做叙述了,黑马程序员上有一个基础的视频教学,可以跟着学习一 ...