题目链接:https://vjudge.net/contest/239445#problem/E

 
 

E -

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个字符串,每个字符串由(和)组成,问你是否能用所有字符串凑在一起刚好消掉  ·····()就是可以消掉
个人思路:这题我也不会,感觉很复杂,后来补题,大概思路就是在输入的时候就把能消去的消去,然后最后剩下的一定是(((((或者)))))或者))))((((这三种。你可以先计算出只有(的总共有多少
个),然后就是只剩下)))))和)))(((((这两种类型了,每次把有最多个数的(存起来···(这里说的(是说已经减去了自己拥有的)剩下的(    )   ·····就是这种贪心思想,具体看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=2e2+;
const int maxk=+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
typedef pair<int,int>P;//用来存储有多少个(和)first是(的个数,second是)的个数
P p[maxk];
char s[maxk];
bool vis[maxk];
int n;
P solve(char s[])//这一步操作是消去本来该字符串就能消去的()
{//这里学到了,竟然是用pair型来定义函数,长见识了
int l=,r=;
for(int i=;s[i];i++)
{
if(s[i]=='(') l++;
else if(l) l--;
else r++;
}
return P(l,r);//返回值也应当是pair型
}
int main()
{
ios::sync_with_stdio(false);
cin>>n;
int cnt=;//用来存储只有(的字符串中( 的总个数
for(int i=;i<n;i++)
{
cin>>s;
p[i]=solve(s);
if(!p[i].second) cnt+=p[i].first,i--,n--;//存储下来了直接删掉
}
int ans=,ret=;//ans用来存储有多少个)
memset(vis,false,sizeof(vis));//用来标记字符串是否已经用过了
int mx,id;
for(int i=;i<n&&ret;i++)
{
mx=-,id=-;
for(int j=;j<n;j++)
{
if(vis[j]||ans<p[j].first) continue;//这里为何要ans>p[j].first,因为如果>的话,那么本个字符串后面的(全部可以消除
int k=p[j].second-p[j].first;//这是消去了(还剩多少个)
if(k>mx)
{
mx=k;
id=j;//这里用到了点贪心,就是每次都保留最多的),
}
}
if(id==-) ret=;//如果还没到最后一个字符串就已经没有选择了,代表消不了,直接退出
vis[id]=true;
ans+=mx;
}
if(ans==cnt&&ret) cout<<"Yes"<<endl;//不仅仅ans=cnt,ret也要刚好为1
else
cout<<"No"<<endl;
return ;
}

Problem Statement的更多相关文章

  1. C#学习日志 day10 -------------- problem statement

    Revision History Date Issue Description Author 15/May/2015 1.0 Finish most of the designed function. ...

  2. URAL 1881 Long problem statement

    1881. Long problem statement Time limit: 0.5 secondMemory limit: 64 MB While Fedya was writing the s ...

  3. Google Code Jam 2010 Round 1A Problem A. Rotate

    https://code.google.com/codejam/contest/544101/dashboard#s=p0     Problem In the exciting game of Jo ...

  4. Google Code Jam 2010 Round 1B Problem A. File Fix-it

    https://code.google.com/codejam/contest/635101/dashboard#s=p0   Problem On Unix computers, data is s ...

  5. http://codeforces.com/problemset/problem/594/A

    A. Warrior and Archer time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. Codeforces Round #296 (Div. 1) B. Clique Problem 贪心

    B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle

    Problem A permutation of size N is a sequence of N numbers, each between 0 and N-1, where each numbe ...

  8. Google Code Jam 2014 资格赛:Problem B. Cookie Clicker Alpha

    Introduction Cookie Clicker is a Javascript game by Orteil, where players click on a picture of a gi ...

  9. 0/1 knapsack problem

    Problem statement Given n items with size Ai and value Vi, and a backpack with size m. What's the ma ...

随机推荐

  1. Linux ssh 不需要输入密码的方法

    采用证书的方法可以解决ssh不需要输入密码的问题. 本文采用CentOS的操作系统,创建SSH的key,并在两台或多台机器间实现信任.从而实现SSH登录不需要输入密码的功能. 首先,在一台机器上创建S ...

  2. CSS如何计算优先级?如何计算权重?

    (1) 优先级就近原则,同权重以最近者为准 载入样式以最后载入的样式为准: 同权重下:内联样式表(标签内部) > 嵌入样式表(当前文件) > 外部样式表(外部文件) !import > ...

  3. C/C++中变量类型最值之宏定义

    C/C++ [climits(limits.h)] CHAR_BIT        Number of bits for a char object (byte)                    ...

  4. Matlab数据类型的转换

    Matlab中有15种基本数据类型,主要是整型.浮点.逻辑.字符.日期和时间.结构数组.单元格数组以及函数句柄等. 1.整型:(int8:uint8:int16:uint16:int32:uint32 ...

  5. ubuntu使用root权限登录的设置方法

    Ubuntu系统默认是不允许用户以root身份登录的,在网上找到的方法如下: 1.首先设置root密码,利用现有管理员帐户登陆Ubuntu,在终端执行命令:sudo passwd root,接着输入密 ...

  6. 各版本Google浏览器下载地址

    各版本谷歌浏览器下载地址 https://www.chromedownloads.net/chrome64win/

  7. Entity Framework Code-First(10):Fluent API

    Fluent API in Code-First: We have seen different DataAnnotations attributes in the previous sections ...

  8. UITableViewCell 的复用机制

    cell重用机制 http://blog.cnrainbird.com/index.php/2012/03/20/guan_yu_uitableview_de_cell_fu_yong_tan_tan ...

  9. python接口自动化(三十五)-封装与调用--流程类接口关联(详解)

    简介 流程相关的接口,主要用 session 关联,如果写成函数(如上篇),s 参数每个函数都要带,每个函数多个参数,这时候封装成类会更方便.在这里我们还是以博客园为例,带着小伙伴们实践一下. 接口封 ...

  10. SCUT - 223 - Maya - 构造

    https://scut.online/p/223 给定两个数N,M,构造M个在[0,80000]以内的互不相同的数使之异或和为N. 首先特判一下M<=2的两个简单情况,还有坑爹的-1! 然后想 ...