CodeForces 1059B(思维)

Accordion
time limit per test:3 seconds
memory limit per test:256 megabytes
input :standard input
output :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
input
|[a: b:|]
output
4
input
|]:[|:]
output
-1

理解:
这应该算是一个思维型的题目 看到字符串 很多人也许会想到括号匹配 用栈去实现这道题 其实不用那么麻烦 仔细想 括号匹配要求每一个元素都要去寻找与其对应的另一个匹配元素 但这道题其实要求的只是找到四个标志而已 所以我们声明四个标志 分别遍历数组 如果存在的话 则以两个冒号的坐标为边界 寻找 ‘|’ 记录个数 不存在的话我们就输出-1
至于为什么要分别遍历 因为 ’ ] ’ 这个符号又可以出现不止一次 如果我们不分别遍历 则有可能出现错误
ps:这个代码写的有点傻
以下是AC代码:

#include<stdio.h>
#include<string.h>
int main()
{
	char tmp[500100];
	int p1=-1,p2=-1,p3=-1,p4=-1,flag=4;
	gets(tmp);
	for(int i=0;i<strlen(tmp);i++)
	{
		if(tmp[i]=='[')
		{
			p1=i;
			break;
		}
	}
	if(p1==-1)
	{
		printf("-1");
		return 0;
	}
	for(int i=p1+1;i<strlen(tmp);i++)
	{
		if(tmp[i]==':')
		{
			p2=i;
			break;
		}
	}
	if(p2==-1)
	{
		printf("-1");
		return 0;
	}
	for(int i=strlen(tmp)-1;i>0;i--)
	{
		if(tmp[i]==']')
		{
			p4=i;
			break;
		}
	}
	if(p4==-1 || p4<p2)
	{
		printf("-1");
		return 0;
	}
 	for(int i=p4-1;i>0;i--)
	{
		if(tmp[i]==':')
		{
			p3=i;
			break;
		}
	}
	if(p3==-1 || p3==p2)
	{
		printf("-1");
		return 0;
	}
	for(int i=p2+1;i<=p3;i++)
	{
		if(tmp[i]=='|')
		flag++;
	}
	printf("%d",flag);
	return 0;
} 
posted @ 2022-07-02 13:18  李兆龙的博客  阅读(20)  评论(0编辑  收藏  举报