有点贪心

 

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


复制代码
 1 //  思维+贪心
 2 // Aizu - 2681 
 3 #include <iostream>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 #include <utility>
 9 #include <vector>
10 #include <map>
11 #include <queue>
12 #include <stack>
13 #include <cstdlib>
14 typedef long long ll;
15 #define lowbit(x) (x&(-x))
16 #define ls l,m,rt<<1
17 #define rs m+1,r,rt<<1|1
18 using namespace std;
19 const int N=1e2+9;
20 int n;
21 char s[N];
22 typedef pair<int,int>P;
23 P p[N];
24 P solve(char s[])
25 {
26     int l=0,r=0;
27     int i;
28     for(i=0;s[i];i++){
29         if(s[i]=='(')  l++;
30         else if(l)  l--;
31         else r++;
32     }
33     return P(l,r);//l为( 数目,r为)数目
34 }
35 bool vis[N];
36 int main()
37 {
38     scanf("%d",&n);
39     int  cnt=0;
40     for(int i=0;i<n;i++)
41     {
42         scanf("%s",s);
43         p[i]=solve(s);
44         if(!p[i].second) cnt+=p[i].first,i--,n--;
45         // cnt 为只有(的字符串的(的和
46     }
47      int ans=0,ret=1;
48      memset(vis,0,sizeof(vis));
49      int mx,id;
50      for(int i=0;i<n&&ret;i++){
51          id=-1,mx=-120;
52          for(int j=0;j<n;j++)
53          {
54              if(vis[j]||ans<p[j].first) continue;
55              int k=p[j].second-p[j].first;
56              if(mx<k) mx=k,id=j;
57          }
58          //第一个放在最右边 ,依次往左(前)放。
59         // mx 提供最多的右括号,(贪心)
60          if(id==-1)  ret=0;
61          vis[id]=1;
62          ans+=mx;//ans 为这个字符串的净提供右括号数目。
63      }
64     // printf("%d %d %d\n",ret,ans,cnt);
65      if(ret&&ans==cnt)  printf("Yes\n");
66      else printf("No\n");
67      return 0;
68 }
复制代码

 




posted on   cltt  阅读(182)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示