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的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. 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 ...

  3. Stack Overflow is a question and answer site

    http://stackoverflow.com/ _ Stack Overflow is a question and answer site for professional and enthus ...

  4. SPOJ GSS2 Can you answer these queries II

    Time Limit: 1000MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description Being a ...

  5. Question and Answer

    1.VS2013使用EntityFrame问题解决办法 解决办法参照博客http://pinter.org/?p=2374 使用到EntityFrame的项目配置文件修改如下: 项目中凡是使用到DbC ...

  6. 转 https://www.zhihu.com/question/27606493/answer/37447829

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:梁川链接:https://www.zhihu.com/question/27606493/answer/37447829来源: ...

  7. hdu 4027 Can you answer these queries?

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...

  8. GSS4 2713. Can you answer these queries IV 线段树

    GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...

  9. GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树

    GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...

随机推荐

  1. node.js + expres 的安装

    一 windows下安装 首先去官网下载msi安装包. 两篇很有参考价值的文章: http://cnodejs.org/topic/4fae80c02e8fb5bc650a8360 http://bl ...

  2. Sublime Text的常用插件

    工欲善其事,必先利其器.好的插件会更多的帮助我们装逼. 最新感悟:也不要装太多的插件.有些无用的插件可以删除,有时反而臃肿.bloger下载官方最新稳定版st3 3126下载下来25M左右.安装十来个 ...

  3. 关于引用JS和CSS刷新浏览器缓存问题

    有时候我们会碰到上线的新版本都要刷新一次缓存的问题.那是因为改了JS的内容,但是JSP引用的地方后面的字符串未发生改变导致浏览器读取浏览器缓存而不会重新加载新的JS内容,以下提供两种解决方式: 1.每 ...

  4. phpcms 中路径问题

    <table width="100%" border="0" cellspacing="0" cellpadding="0& ...

  5. Momentics创建Photon图形程序

    Photon microGui是qnx原生的UI图形工具.Qnx下开发Photon 一般是使用phAB来创建,使用默认的Momentics IDE也可以创建Photon图形程序. 首先需要创建一个c/ ...

  6. [Amazon] Amazon IAP for Unity

    1> 下载amazon IAP3.0 for unity plugin 2> 根据 https://developer.amazon.com/public/apis/earn/in-app ...

  7. Node聊天程序实例02:chat_server.js

    作者:vousiu 出处:http://www.cnblogs.com/vousiu 本实例参考自Mike Cantelon等人的<Node.js in Action>一书. chat_s ...

  8. 转载分享----一线交付眼中的为何"项目总是迟迟无法交付”

    当初博主在一线交付BOSS系统中承担过TC角色 交付的路途很艰辛,加班到10点多或1点多第二天8点上班,还有通宵的日子 还有无数个问题从开始到关闭的周期,各种催人,各种掐架拉会,各种被甲方嫌弃 看到这 ...

  9. [Java] Java解析XML格式Response后组装成Map

    //Get and Parse Response def response = context.expand(‘${TestStepName#Response}’) def xmlParser = n ...

  10. 【图像】Matlab图像标定工具箱

    参考教程: Matlab工具箱教程  http://www.vision.caltech.edu/bouguetj/calib_doc/ 摄像机模型  http://oliver.zheng.blog ...