zoukankan      html  css  js  c++  java
  • Aizu

    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 (1n1001≤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
    ");
    	else
    	printf("No
    ");
    	return 0;
    }
    

      



  • 相关阅读:
    [调参]batch_size的选择
    [调参]CV炼丹技巧/经验
    [Pytorch]Pytorch加载预训练模型(转)
    [PyTorch]论文pytorch复现中遇到的BUG
    [Opencv]图像的梯度与边缘检测(转)
    freemodbus移植、实例及其测试方法
    eclipse的C/C++开发搭建
    ROS安装
    U-boot移植
    QT开发实战一:图片显示
  • 原文地址:https://www.cnblogs.com/cglongge/p/9358680.html
Copyright © 2011-2022 走看看