[codeforces 1305B] Kuroni and Simple Strings 有点象快排

Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated, T-shirts + prizes!)   比赛人数7420

[codeforces 1305B] Kuroni and Simple Strings   有点象快排

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.ml/contest/1305/problem/B

Problem Lang Verdict Time Memory
B - Kuroni and Simple Strings GNU C++11 Accepted 31 ms 0 KB

看看代码挺简单,但是看着题目,将代码编写出来,就不简单。

delete these bold characters from the string and to get '))()'.若能接着这句再继续说,还需进行一次操作,删除(),才能得到Kuroni想要的字串)),那该有多好。

()() are not simple.能说明,()()不是Kuroni想要的字串,那该有多好。

Input:

(()())

Output:

1
4
1 2 5 6 

还有一种删法

1

6

1 2 3 4 5 6

该题,就容易上手了。

意见,陌生度大的题,就要做更详细的说明。

AC代码如下

#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 1010
using namespace std;
int ans[maxn],tot;
char s[maxn];
int main(){
	int l,r,len,i;
	scanf("%s",s+1);
	len=strlen(s+1);
	l=1,r=len;
	while(1){
		while(l<=len&&s[l]==')')l++;
		while(1<=r&&s[r]=='(')r--;
		if(l>r)break;//绝不可能出现l==r的情况。
		ans[++tot]=l,ans[++tot]=r;
		l++,r--;
	}
	if(!tot)printf("0\n");
	else{
		printf("1\n");
		printf("%d\n",tot);
		sort(ans+1,ans+1+tot);
		for(i=1;i<tot;i++)printf("%d ",ans[i]);
		printf("%d\n",ans[tot]);
	}
	return 0;
}
发布了567 篇原创文章 · 获赞 538 · 访问量 45万+

猜你喜欢

转载自blog.csdn.net/mrcrack/article/details/104650450