Kuroni and Simple Strings(补题)~解题报告

Kuroni and Simple Strings

题目描述:

Now that Kuroni has reached 10 years old, he is a big boy and doesn’t like arrays of integers as presents anymore. This year he wants a Bracket sequence as a Birthday present. More specifically, he wants a bracket sequence so complex that no matter how hard he tries, he will not be able to remove a simple subsequence!

We say that a string formed by nn characters ‘(’ or ‘)’ is simple if its length nn is even and positive, its first n2n2 characters are ‘(’, and its last n2n2 characters are ‘)’. For example, the strings () and (()) are simple, while the strings )( and ()() are not simple.

Kuroni will be given a string formed by characters ‘(’ and ‘)’ (the given string is not necessarily simple). An operation consists of choosing a subsequence of the characters of the string that forms a simple string and removing all the characters of this subsequence from the string. Note that this subsequence doesn’t have to be continuous. For example, he can apply the operation to the string ‘)()(()))’, to choose a subsequence of bold characters, as it forms a simple string ‘(())’, delete these bold characters from the string and to get ‘))()’.

Kuroni has to perform the minimum possible number of operations on the string, in such a way that no more operations can be performed on the remaining string. The resulting string does not have to be empty.

Since the given string is too large, Kuroni is unable to figure out how to minimize the number of operations. Can you help him do it instead?

A sequence of characters aa is a subsequence of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters.

Input:

The only line of input contains a string ss (1|s|10001|s|1000) formed by characters '(' and ')', where |s||s| is the length of ss.

Output:


In the first line, print an integer kk  — the minimum number of operations you have to apply. Then, print 2k2k lines describing the operations in the following format:

For each operation, print a line containing an integer mm  — the number of characters in the subsequence you will remove.

Then, print a line containing mm integers 1≤a1<a2<<am1≤a1<a2<<am  — the indices of the characters you will remove. All integers must be less than or equal to the length of the current string, and the corresponding subsequence must form a simple string.

If there are multiple valid sequences of operations with the smallest kk, you may print any of them.

Sample Input:

(()((

Sample Output:

1
2
1 3 

Sample Input:

)(

Smaple Output:

0

Sample Input:

(()())

Sample Output:

1
4
1 2 5 6

题目大意:

这里只想吐槽一下,题目描述中的Open和close分别代表的是右括号和左括号,自己翻译的时候,一直看着这个东西不知道讲什么,然后题目给了一位小朋友需要一个特定序列括号做生日礼物(这个括号必须是最简单的无法继续操作的括号),什么是简单括号无法操作的呢,比如“()”这种就是最简单无法分割,而例:“)(”这种就是不简单的,给了一个长序列的括号串,让你删除其中的子序列达到生日礼物的要求,输出要操作的最小次数,以及所要删的数目,以及要删的括号下标。

思路分析:

这里讲了最小操作数一个坑,如果一开始给的长序列里面子序列没有一个是简单括号,例如:“)(”这种就直接输出操作为0就行了,然后如果有简单括号序列,那么他的最小操作次数绝对是1(因为你们每次正常操作次数肯定超过或等于1的),这道题其实不太难,难在理解题目意思,那么我们只要把每一个左括号和右括号匹配,将其放在一个列表中,说明其是要删掉的,然后当删除后的序列中没有简单括号的时候,就完成了,如果没完成,继续寻找匹配括号,将其放在列表中。

代码:


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
int a[1100],b[1100];//分别记录右括号和左括号的下标 
int ta=0,tb=0;//记录右括号和左括号的数目 
int c1[1100];//储存匹配的括号 
int main()
{
	string s;
	cin >> s;//得到长序列 
	for(int i=0;i<s.length();i++)
	{
		if(s[i]=='(')//记录 右括号下标 
		{
			a[ta++]=i; 
		}
		else//记录左括号下标 
		{
			b[tb++]=i;
		}
	}
	int la=tb-1,lb=0,t=0;//t为列表中所匹配的数目 
	while(la>=0 && lb<ta && a[lb]<b[la])//匹配括号字符串 
	{
		 c1[t++]=a[lb++];
		 c1[t++]=b[la--];		
	}
	if(t)//如果列表中无匹配元素,说明长序列都是不简单括号 
	{
		int cnt=0;
		printf("1\n%d\n",t);
		for(int i=0;i<t;i++)//输出所要删除的下标,这里的下标从0开始,所以要加1 
		{
			printf(cnt++==0?"%d ":"%d",c1[i]+1);
		}
	}
	else
	printf("0\n");
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值