Problem Statement

You are given nn strings str1,str2,…,strnstr1,str2,…,strn, each consisting of ( and ). The objective is to determine whether it is possible to permute the nn strings so that the concatenation of the strings represents a valid string.

Validity of strings are defined as follows:

  • The empty string is valid.
  • If AA and BB are valid, then the concatenation of AA and BB is valid.
  • If AA is valid, then the string obtained by putting AA in a pair of matching parentheses is valid.
  • Any other string is not valid.

For example, "()()" and "(())" are valid, while "())" and "((()" are not valid.

Input

The first line of the input contains an integer nn (1≤n≤1001≤n≤100), representing the number of strings. Then nn lines follow, each of which contains stristri (1≤|stri|≤1001≤|stri|≤100). All characters in stristri are (or ).

Output

Output a line with "Yes" (without quotes) if you can make a valid string, or "No" otherwise.

Sample Input 1

3
()(()((
))()()(()
)())(())

Output for the Sample Input 1

Yes

Sample Input 2

2
))()((
))((())(

Output for the Sample Input 2

No
题意:给你n个字符串(只包含 “(” 和 “)” ),问是否能通过排序使得这n个字符串组成的大字符串合法
思路:根据题目给出的字符串,我们先可以对它进行预处理,消除掉已经配对的左右括号,最后得到的一定是像“))(((”这样的字符串,然后我们就可以把它分为右括号多和左括号多的两个数组(假设他们
分别是数组a和数组b),我们可以分别对这两个数组中的左括号数和右括号数进行排序(为什么我们要对它进行排序呢,对于数组a,它里面所有的数据都是右括号数大于左括号数,所有我们要想让它成为
一个合法的序列,我们就要尽可能的让少的左括号来与当前多出来的右括号来配对(如果你当前的要配对的左括号的数量大于多出来的右括号,剩下的没有被配对的左括号就消除不了了,则它不可能会是一个
合法的序列)),最后就看a数组进行消除后剩下的右括号数是否等于b数组进行消除后剩下的左括号数。
代码:
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
struct st{
int x,y;
bool operator <(const st& a)const{//重载小于号
return x<a.x;
}
};
char c[110];
vector<st> a,b;
int main(){
int n;
int len;
int l,r;
int i,j;
st k;
scanf("%d",&n);
for(i=0;i<n;i++){
getchar();
scanf("%s",c);
len=strlen(c);
l=r=0;
for(j=0;j<len;j++){//进行预处理,消除可以配对的括号
if(c[j]==')'){
if(r!=0)
r--;//记录左括号数
else
l++;//记录右括号数
}
else
r++;
}
if(l>r){//左括号多 ,放入b数组
k.x=r;
k.y=l;
b.push_back(k); }
else{//右括号多
k.x=l;
k.y=r;
a.push_back(k);
}
}
sort(a.begin(),a.end());//从小到大排序
sort(b.begin(),b.end());
int lg=1;//标记它是否合法
int a1,b1;
a1=b1=0;
for(i=0;i<a.size()&≶i++){
if(a1<a[i].x)//当前没配对的右括号数不能把当前放入的左括号全部抵消掉,不合法
lg=0;
else
a1=a1-a[i].x+a[i].y;//消除配对的括号
}
for(i=0;i<b.size()&≶i++){//同上
if(b1<b[i].x)
lg=0;
else
b1=b1-b[i].x+b[i].y;
}
if(a1==b1&&lg)//最后剩余的左右括号数量要相等
printf("Yes\n");
else
printf("No\n");
return 0;
}

  

												

Aizu - 2681(括号匹配)的更多相关文章

  1. 括号匹配 区间DP (经典)

    描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...

  2. YTU 3003: 括号匹配(栈和队列)

    3003: 括号匹配(栈和队列) 时间限制: 1 Sec  内存限制: 128 MB 提交: 2  解决: 2 [提交][状态][讨论版] 题目描述 假设一个表达式中只允许包含三种括号:圆括号&quo ...

  3. [原]NYOJ 括号匹配系列2,5

    本文出自:http://blog.csdn.net/svitter 括号匹配一:http://acm.nyist.net/JudgeOnline/problem.php?pid=2 括号匹配二:htt ...

  4. POJ C程序设计进阶 编程题#4:括号匹配问题

    编程题#4:扩号匹配问题 来源: POJ(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 在某 ...

  5. 南阳理工ACM 括号匹配问题,并求出使得括号能够匹配需要新增的最小括号数(括号匹配(二))

    描述 给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起 ...

  6. 【栈思想、DP】NYOJ-15 括号匹配(二)

    括号匹配(二) 描述 给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能 ...

  7. NYOJ 题目15 括号匹配(二)(区间DP)

    点我看题目 题意 : 中文题不详述. 思路 : 本来以为只是个小模拟,没想到是个区间DP,还是对DP不了解. DP[i][j]代表着从字符串 i 位置到 j 位置需要的最小括号匹配. 所以初始化的DP ...

  8. C语言数据结构之栈:括号匹配

    括号匹配这是个很简单的题目,如果只有小括号,就模拟进栈和出栈的过程就行了: 注:输入时'@'作为结束标志 #include <stdio.h> int main() { freopen(& ...

  9. [NYOJ 15] 括号匹配(二)

    括号匹配(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:6   描述 给你一个字符串,里面只包含"(",")","[&qu ...

随机推荐

  1. Binary Gap(二进制空白)

    中文标题[二进制空白] 英文描述 A binary gap within a positive integer N is any maximal sequence of consecutive zer ...

  2. logstash配置文件

    1. 安装  logstash 安装过程很简单,直接参照官方文档: https://www.elastic.co/guide/en/logstash/current/installing-logsta ...

  3. 正睿 2018 提高组十连测 Day4 T3 碳

    记'1'为+1,'0'为-1; 可以发现 pre[i],suf[i]分别为前/后缀和 a[i]=max(pre[l.....i]); b[i]=max(suf[i+1....r]); ans=max( ...

  4. jquery 根据自定义属性选择

    <div myattr="test">text</div> 使用$("div[myattr='test']")进行选择

  5. 树莓派 CSI摄像头 No data received from sensor. Check all connections, including the Sunny one on the camera board

    不知道为什么摄像头在包里放了两天旧坏了,中间完全没用过摄像头的功能,查了资料,原因大概有两种 1)sunny烧了 2)试摄像头传感器坏了 这两天没有插拔过摄像头,可能是树莓派漏电了,也可能是它被压坏了 ...

  6. 文件名简体转繁体bat

    @echo off rem 指定文件夹路径 set "fd=D:\下载的图片" rem 0为转换文件名,1为转换文件夹名,2为同时转换文件名和文件夹名 set f=0 rem 0为 ...

  7. LVM(逻辑卷管理)从0到实战

    一.请看图 二.LVM——Logical Volume Manager LVM就是动态卷管理,可以将多个硬盘和硬盘分区做成一个逻辑卷,并把这个逻辑卷作为一个整体来统一管理,动态对分区进行扩缩空间大小, ...

  8. 3月23 格式布局及relative

    主要是针对格式布局的一些内容: 1:position:fix 锁定位置(相对于浏览器的位置),例如网上弹出的一些广告 <style type="text/css"> # ...

  9. 深拷贝的原生js实现

    面试时被问到怎么实现深拷贝,想都没想就用var obj2=JSON.parse(JSON.stringify(obj1))来实现.但面试官却要我用循环写出来,那就只能用递归了.可惜当时一下子忘了判断是 ...

  10. stl中的transform()注意其与for_each的不同点(有无返回值)

    #include<iostream> using namespace std; #include"vector" #include"algorithm&quo ...