1745. Yet Another Answer
http://acm.timus.ru/problem.aspx?space=1&num=1745
题目大意:
可以是任意的顺序,在满足括号匹配的情况下,求组合成的字符串长度最长
思路:
先将每一个字符串进行处理,处理时将匹配的去掉 比如说 { )(()(())(} 处理后就变成了 {)( (}
当然字符串的长度是没有变化的 处理后的字符串假如说 左括号的个数为L 右括号的个数为R
我们必须确定一个正确的顺序,可以让我们从头到尾的逐个决定是否选用当前字符串,而且不影响最优结果
字符串有 L>R,L<R 和 L=R 三种情况,对于L>R的字符串要放在前面 因为每一个这样的字符串都可以使
左括号 — 右括号 的值变大,
而对于 L<R 的情况对应放在最后,L=R的情况放在中间
对于L<R的字符串之间 需要把 R 小的放在前面, 对于L>R的情况 要用对称思想去确定顺序
确定顺序之后,剩下的就DP就可以了
代码:
#include<iostream>
#include<stack>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<cmath> using namespace std; typedef long long ll;
typedef pair<int,int> pp;
const double eps=1e-9;
const int INF=0x3f3f3f3f;
const int N=1002;
const int M=10002;
struct node
{
string s;
int lk,rk;
int index;
}x[N];
int dp[N][M];
int func(const node& a)
{
if(a.lk-a.rk>0) return 1;
if(a.lk-a.rk<0) return -1;
return 0;
}
bool cmp(const node& a,const node& b)
{
int lf=func(a),rf=func(b);
if(lf==1&&rf==1)
return a.rk<b.rk;
if(lf==-1&&rf==-1)
return a.lk>b.lk;
return lf>rf;
}
int main()
{
//freopen("data.in","r",stdin);
int n;
while(cin>>n)
{
for(int i=0;i<n;++i)
{
cin>>x[i].s;
x[i].index=i+1;
stack<char>st;
for(unsigned int j=0;j<x[i].s.size();++j)
{
if(st.empty()||x[i].s[j]=='('||st.top()==')')
{
st.push(x[i].s[j]);
}else
{
st.pop();
}
}
x[i].lk=x[i].rk=0;
while(!st.empty())
{
if(st.top()=='(') ++x[i].lk;
else ++x[i].rk;
st.pop();
}
}
sort(x,x+n,cmp); memset(dp,-1,sizeof(dp));
dp[0][0]=0;
for(int i=0;i<n;++i)
{
for(int j=0;j<M;++j)
if(dp[i][j]!=-1)
{
dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
if(j>=x[i].rk)
dp[i+1][j-x[i].rk+x[i].lk]=max(dp[i+1][j-x[i].rk+x[i].lk],(int)(dp[i][j]+x[i].s.size()));
}
}
vector<int>vt;
int i=n,j=0;
while(i>0)
{
if(dp[i-1][j]==dp[i][j])
i=i-1;
else
{
vt.insert(vt.begin(),i-1);
j=j-x[i-1].lk+x[i-1].rk;i=i-1;
}
}
cout<<dp[n][0]<<" "<<vt.size()<<endl;
for(unsigned int i=0;i<vt.size();++i)
{
if(i>0) cout<<" ";
cout<<x[vt[i]].index;
}
cout<<endl; }
return 0;
}
1745. Yet Another Answer的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- Stack Overflow is a question and answer site
http://stackoverflow.com/ _ Stack Overflow is a question and answer site for professional and enthus ...
- SPOJ GSS2 Can you answer these queries II
Time Limit: 1000MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description Being a ...
- Question and Answer
1.VS2013使用EntityFrame问题解决办法 解决办法参照博客http://pinter.org/?p=2374 使用到EntityFrame的项目配置文件修改如下: 项目中凡是使用到DbC ...
- 转 https://www.zhihu.com/question/27606493/answer/37447829
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:梁川链接:https://www.zhihu.com/question/27606493/answer/37447829来源: ...
- hdu 4027 Can you answer these queries?
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
随机推荐
- 自己动手写Logistic回归算法
假设一个数据集有n个样本,每个样本有m个特征,样本标签y为{0, 1}. 数据集可表示为: 其中,x(ij)为第i个样本的第j个特征值,y(i)为第i个样本的标签. X矩阵左侧的1相当于回归方程的常数 ...
- 23-React Render Element
第23节 React Render Element 1.Element 元素是反应应用程序的最小积木. 元素描述你在屏幕上看到的内容.: const element= <h1>你好,世界& ...
- Button的enabled和clickabled的区别
Button的enabled和clickabled的区别 setEnable设置用户是否可以点击此按钮,setClickable设置程序在某个条件下自动点击此按钮
- Mac下遇到 'reading initial communication packet’ 问题
今天在开发过程中,一个单位跑的好好的项目,在家中的Mac下运行时,遇到了下面这个错误: "Lost connection to MySQL server at 'reading init ...
- 小谈 checkbox 的选中
先做草稿,稍后完善, javascript for (var i = 0; i < jsonmsg.length; i++) { var ischecked = false; for (var ...
- 1019: [SHOI2008]汉诺塔
1019: [SHOI2008]汉诺塔 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1495 Solved: 916[Submit][Status] ...
- 解决 java 使用ssl过程中出现"PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"
今天,封装HttpClient使用ssl时报一下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExc ...
- Java知识积累2-StringReverse实现文字(单词)倒叙输出
package String; import java.util.Stack;import java.util.StringTokenizer; public class StringReverse ...
- php访问远程服务器上的文件
test.php <?php $fp=fopen('http://www.baidu.com', 'r'); while (!feof($fp)) { $chunk=fgets($fp); ec ...
- angular 路由请求js文件
<script type="text/javascript" src="http://apps.bdimg.com/libs/angular.js/1.3.2/an ...