题目:Yuhao and a Parenthesis(思维)

One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.
A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it’s possible to obtain a correct arithmetic expression by inserting characters “+” and “1” into this sequence. For example, the sequences “(())()”, “()” and “(()(()))” are correct, while the bracket sequences “)(”, “(()” and “(()))(” are not correct.
Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.
This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it?

Input
The first line contains one integer n (1≤n≤105) — the number of bracket sequences.
Each of the following n lines contains one bracket sequence — a non-empty string which consists only of characters “(” and “)”.
The sum of lengths of all bracket sequences in the input is at most 5⋅105.
Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn’t matter.

Output
Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement.

思路:
一开始想着两重循环,第一重i从1到n,第二重i+1到n,并且用一个book数组标记两个字符串是否匹配并使用,在循环里面判断两次,两个字符串两次的链接情况进行分析,但是这个需要的空间太大了,需要开一个s[100000][500000]数组,所以这个就不行了。后来看题解,每种字符串可以先处理一下,自己匹配后,只剩下集中情况了,分别是:空、…)))、(((…、…))((…
两个空的可以相互匹配,第二三种情况也可以相互匹配,第四种情况不可能匹配。

代码:

//错误
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
char str[100005][100];
char stackk[20];
int book[100005], n;
int main()
{
	int count=0;
	scanf("%d", &n);
	for (int i=1; i<=n; i++)
		scanf("%s", str[i]);
	for (int i=1; i<=n; i++){
		for (int j=i+1; j<=n; j++){
			if (book[i]==0 && book[j]==0){
				int f1=0, f2=0, k;
				memset(stackk, 0, sizeof(stackk));
				strcpy(stackk, str[i]);
				strcat(stackk, str[j]);
				for (k=0; k<strlen(stackk); k++){
					if (stackk[k]=='(')	f1++;
					if (stackk[k]==')')	f2++;
					if (f2>f1)	break;
				}
				if (k==strlen(stackk) && f1==f2){
					count++;
					book[i]=1;
					book[j]=1;
					continue;
				}
				f1=0, f2=0;
				memset(stackk, 0, sizeof(stackk));
				strcpy(stackk, str[j]);
				strcat(stackk, str[i]);
				for (k=0; k<strlen(stackk); k++){
					if (stackk[k]=='(')	f1++;
					if (stackk[k]==')')	f2++;
					if (f2>f1)	break;
				}
				if (k==strlen(stackk) && f1==f2){
					count++;
					book[i]=1;
					book[j]=1;
					continue;
				}
			}
		}
	}
	printf("%d", count);
	return 0;
}
//正确
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
char s[500005];
int l[500005], r[500005];

int main()
{
	int n, count=0, ans=0;
	scanf("%d", &n);
	for (int i=1; i<=n; i++){
		scanf("%s", s);
		int len=strlen(s);
		int left=0, right=0;
		for (int j=0; j<len; j++){
			if (s[j]=='(')
				left++;
			else{
				if (left>0)	left--;
				else	right++;
			}
		}
		if (left>0 && right>0)	continue;
		if (left==0 && right==0)	count++;
		if (left==0 && right>0)	r[right]++;
		if (left>0 && right==0)	l[left]++;	
	}
	for (int i=1; i<=500005; i++)
		ans+=min(r[i], l[i]);
	ans+=count/2;
	printf("%d", ans);
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

H4ppyD0g

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值