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的路径上所有节点的权 ...
随机推荐
- Install Ansible on Mac OSX
from: https://devopsu.com/guides/ansible-mac-osx.html and : https://devopsu.com/guides/ansible-post- ...
- yum安装nginx
1.在/etc/yum.repos.d/目录下创建一个源配置文件ngxin.repo: cd /etc/yum.repos.d/ vim nginx.repo 填写如下内容: [nginx] name ...
- 利用Javascript判断操作系统的类型实现不同操作系统下的兼容性
原文地址 http://www.jb51.net/article/33640.htm 在通过Javascript实现客户端和服务端的交互时,有时候需要对操作系统进行判断,以便实现不同操作系统下的兼容性 ...
- php数据类型及转换
- logback 配置详解(一)——logger、root
1.根节点<configuration>包含的属性 scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true. scanPeriod: 设置监测配置文件 ...
- Android微信智能心跳方案 (转)
原创 2015-08-17 phoenix WeMobileDev 前言:在13年11月中旬时,因为基础组件组人手紧张,Leo安排我和春哥去广州轮岗支援.刚到广州的时候,Ray让我和春哥对Line和W ...
- 【转】linux下安装ssh服务器端及ssh的安全配置
一.在服务器上安装ssh的服务器端. $ sudo apt-get install openssh-server 2. 启动ssh-server. $ /etc/init.d/sshrestart 3 ...
- /boot/grub/device.map【设备映射】
grub-install 安装 GRUB 在第一个硬盘的 MBR: # grub-install '(hd0)' grub-install 会先搜寻设备对应的文件(/boot/grub/device. ...
- WPF中RDLC报表的钻取实现
1.新建wpf项目,并引入3个程序集: Microsoft.ReportViewer.WinForms WindowsFormsIntegration System.Windows.Forms 如果无 ...
- CSS3--overflow属性
overflow:当内容溢出元素框时发生的事情: overflow:默认,内容不会裁剪,会呈现在元素框之外: overflow:hidden:内容会被裁剪,并且其余部分是不可见的(清除浮动) over ...