题目:Yuhao and a Parenthesis(思维

网友投稿 605 2022-11-26

题目:Yuhao and a Parenthesis(思维)

题目: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 #include #include #include #include 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; kf1) 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; kf1) break; } if (k==strlen(stackk) && f1==f2){ count++; book[i]=1; book[j]=1; continue; } } } } printf("%d", count); return 0;}

//正确#include #include #include #include #include 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; j0) 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;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:KMP模板
下一篇:题目:POJ 1101 Sticks(DFS+剪枝)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~