Educational Codeforces Round 58 (Rated for Div. 2)(B. Accordion)

										 B. Accordion
								 time limit per test3 seconds
							  memory limit per test256 megabytes
									  inputstandard input
									 outputstandard 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 091), a colon (ASCII code 058), some (possibly zero) vertical line characters (ASCII code 124), another colon, and a closing bracket (ASCII code 093). The length of the accordion is the number of characters in it.

For example, [::], [:||:] and [:|||:] are accordions having length 4, 6 and 7. ( : | : ), { : | | : }, [ : ], ] : | | : [ are not accordions.

You are given a string s. 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 s, and if so, what is the maximum possible length of the result?

Input
The only line contains one string s (1≤|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 s, print −1−1. Otherwise print maximum possible length of the resulting accordion.

Examples

input
|[a: b:|]
output
4

input
|]:[|:]
output
-1

思路:从左找“[:”,从右往左找“:]”,这两和找完之后尽管搜索在“:”和“:”之间所有“|”字符的个数,最后再加上4。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
	long long int max=0,i,j,cnt=0,t=0,t1,t2;
	char a[600005];
	cin>>a;
	for(i=0;i<strlen(a);i++)
	{
		if(t==1)
		{
			if(a[i]==':')
			{
				t=2;
				cnt+=1;
				t1=i+1;
				break;
			}
		}
		else
		{
			if(a[i]=='[')
			{
				t=1;
				cnt=1;
			}
		}
	}
	if(t==2)
	{
		for(i=strlen(a)-1;i>=t1;i--)
		{
			if(t==3)
			{
				if(a[i]==':')
				{
					t=4;
					cnt+=1;
					t2=i-1;
					break;
				}
			}
			else
			{
				if(a[i]==']')
				{
					t=3;
					cnt+=1;
				}
			}
		}
	}
	if(t==4)
	{
		for(i=t1;i<=t2;i++)
		{
			if(a[i]=='|')
				cnt++;
		}
		max=cnt;
	}
	if(max>0)
		cout<<max<<endl;
	else
		cout<<"-1"<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值