zoukankan      html  css  js  c++  java
  • 有点贪心

    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
    ",ret,ans,cnt);
    65      if(ret&&ans==cnt)  printf("Yes
    ");
    66      else printf("No
    ");
    67      return 0;
    68 }



  • 相关阅读:
    【CF1523E】Crypto Lights
    【洛谷P3228】数列
    【洛谷P4319】变化的道路
    Educational Codeforces Round 110
    【洛谷P2444】病毒
    一、Java语言基础(1)_走进java——第一个java程序
    android studio 调试smali
    Kickstart Round B 2018
    Proj THUDBFuzz Paper Reading: A Review of Machine Learning Applications in Fuzzing
    Proj THUDBFuzz Paper Reading: Fuzzing: Hack, Art, and Science
  • 原文地址:https://www.cnblogs.com/tingtin/p/9350235.html
Copyright © 2011-2022 走看看