Problem Statement
题目链接: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
#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的更多相关文章
- C#学习日志 day10 -------------- problem statement
Revision History Date Issue Description Author 15/May/2015 1.0 Finish most of the designed function. ...
- URAL 1881 Long problem statement
1881. Long problem statement Time limit: 0.5 secondMemory limit: 64 MB While Fedya was writing the s ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 【转】 Pro Android学习笔记(五一):ActionBar(4):标准和Tab模式
之前,我们学习的Action Bar是标准模式,Tab模式的如下图所示. 对于Tab,我们在Android学习笔记(二二): 多页显示-Tag的使用中学习过,但Action Bar的tab更适合fra ...
- libvirt, libvirt-python, libvirtd 关系浅析
libvirt 官方解释: http://libvirt.org/ 见分隔线以下. 我的理解:libvirt 作为一个中间层,封装了对下层虚拟化 hypervisor 的操作方法.也就是说,无论你是 ...
- openStack kvm 虚拟机CPU颗粒化控制
前一篇理解cpu topology对CPU Topology进行了学习总结,这里想总结下OpenStack下vCPU与pCPU常用的的绑定方式. 在尝试这些绑定之前,尤其是处理NUMA架构时还是建议看 ...
- pig 入门教程(1)
出处:http://www.codelast.com/ 本文可以让刚接触pig的人对一些基础概念有个初步的了解. 本文大概是互联网上第一篇公开发表的且涵盖大量实际例子的Apache Pig中文教程(由 ...
- unreal3控制台窗口属性调整
在windows平台上,unreal3的console窗口类是FOutputDeviceConsoleWindows 启动时,它可以从XXXGame.ini中读取诸如窗口大小之类的属性,具体的代码在 ...
- C#内存管理之托管堆与非托管堆( reprint )
在 .NET Framework 中,内存中的资源(即所有二进制信息的集合)分为“托管资源”和“非托管资源”.托管资源必须接受 .NET Framework 的 CLR (通用语言运行时)的管理(诸如 ...
- Sharepoint2013商务智能学习笔记之Performancepoint service 配置(九)
1)配置Performance Service服务 第一步,新建performance service.先在管理中心,系统设置区域点击管理服务器上的服务,确认Performance Service服务 ...
- 谈谈asp.net中的<% %>,<%= %>,<%# %><%$ %>的使用-转
首先我们来看一下<% %>的使用 在aspx的页面中只能使用服务器控件和一般的控件,有些时候你想在该页面写入c#代码,必须使用<% %>,然后在里面写入c#的代码,下面我们来看 ...
- 【leetcode 76. 最小覆盖子串】解题报告
思路:滑动窗口思想 方法一:滑动窗口 string minWindow(string s, string t) { // 1.tdict记录T中每个字母与字母个数 // 2.维护一个滑动窗口字母的计数 ...
- 前端编码规范 -- css篇
合理的避免使用ID 一般情况下ID不应该被应用于样式. ID的样式不能被复用并且每个页面中你只能使用一次ID. 使用ID唯一有效的是确定网页或整个站点中的位置. 尽管如此,你应该始终考虑使用class ...