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. 渗透杂记-2013-07-13 ms10_061_spoolss

    [*] Please wait while the Metasploit Pro Console initializes... [*] Starting Metasploit Console... M ...

  2. python之前端

    一.Html总结 1.Html简介 HTML 是一种标记语言 忽略大小写,语法宽松 使用 HTML 标记和元素,可以: 控制页面和内容的外观 发布联机文档 使用 HTML 文档中插入的链接检索联机信息 ...

  3. Maven 配置 Selenium + testNG + reportNG 运行环境

    .markdown-preview:not([data-use-github-style]) { padding: 2em; font-size: 1.2em; color: rgb(56, 58, ...

  4. php : 基础(1)

    php基本语法形式 1.php的标记符 有以下形式: 形式1(推荐): <?php 这里要写符合php语法的语句 ?> 形式2: <script language="php ...

  5. [Jenkins] Jenkins配置自动构建时间代表意义

  6. sphinx全文检索 安装配置和使用

    公司项目刚刚导入大量产品数据,然后发现网站的产品搜索很卡,原本是原生sql的like来做模糊搜索,数据量20W的时候还可以接受,但是上百万就很卡了,所以需要做优化. 经过考虑,打算采用全文检索 sph ...

  7. 设置git 不提交 修改权限的文件

    vim .git/config  打开文件

  8. js控制 input框中输入数字时,累计求和

    $('.cc input').bind('input propertychange', function(){ var total = 0; $("input").each(fun ...

  9. ubuntu 跟xshell的问题

    有2个分析: 1:是windos的防火墙没有关闭 2:是虚拟机没有安装sshd服务器 ubuntu在CLI界面下输入:dpkg -l |grep ssh 因为是我安装过的sshd server   要 ...

  10. cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)

    题目: Description standard input/output As most of you know, the Arab Academy for Science and Technolo ...