http://poj.org/problem?id=1239

题意:
给出一串序列,现在要添加逗号作为分隔符,使得序列是递增序列,然后让最后一个数尽量小,第一个数尽量大。

思路:
先从头到尾进行一次dp,d【i】表示分析到第i位时往前的最小长度,这样一来,d【n】就表示最后一位的最小长度。

在满足了最后一位尽量小的情况下,我们再从尾到头进行一次dp,此时d【i】表示分析到第i位时往后的最大长度。思路和第一次dp是差不多的。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; int d[maxn];
char s[maxn]; bool judge(int i, int j, int x, int y) //判断【i,j】是否大于【x,y】
{
int len1=j-i+;
int len2=y-x+; while(s[i]=='') {i++;len1--;}
while(s[x]=='') {x++;len2--;} if(len1>len2) return true;
else if(len1<len2) return false;
else
{
for(int k=; k<len1;k++)
{
if(s[k+i]>s[x+k]) return true;
else if(s[k+i]<s[x+k]) return false;
}
}
return false;
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%s",s+))
{
int n=strlen(s+);
if(n== && s[]=='') break; //从头到尾找最小长度
for(int i=;i<=n;i++)
{
d[i]=i;
for(int j=i-;j>=;j--)
{
if(judge(j+,i,j-d[j]+,j))
{
d[i]=i-j;
break;
}
}
} //从尾到头找最大长度
int t=n-d[n]+; //[t,n]已经是决定了的,不能改变
d[t]=d[n];
for(int i=n-d[n];i>=;i--)
{
if(s[i]=='')
{
d[i]=d[i+]+;
continue;
}
for(int j=t;j>i;j--)
{
if(judge(j,j+d[j]-,i,j-))
{
d[i]=j-i;
break;
}
}
} int tmp=d[]+;
for(int i=;i<=n;i++)
{
if(tmp==i)
{
printf(",");
tmp=d[i]+i;
}
printf("%c",s[i]);
}
printf("\n");
}
return ;
}

POJ 1239 Increasing Sequences(经典的两次dp)的更多相关文章

  1. POJ 1239 Increasing Sequences 动态规划

    题目链接: http://poj.org/problem?id=1239 Increasing Sequences Time Limit: 1000MSMemory Limit: 10000K 问题描 ...

  2. POJ 1239 Increasing Sequences [DP]

    题意:略. 思路:进行两次dp. 第一次dp从前向后,用dp[x]表示从第x位向前dp[x]位可构成一个数字,且与前面的数组符合题意要求.最后求的dp[n]即为最后一个数字的长度. 而题目还有要求,所 ...

  3. POJ 1185 炮兵阵地 经典的 状态压缩dp

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16619   Accepted: 6325 Description ...

  4. TZOJ 5963 Increasing Sequences(线性DP)

    描述 Given a string of digits, insert commas to create a sequence of strictly increasing numbers so as ...

  5. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

  6. UVA 10163 Storage Keepers(两次DP)

    UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...

  7. 【POJ 3140】 Contestants Division(树型dp)

    id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  8. Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5646   Accepted: 1226 Description In an ...

  9. POJ 1321 - 棋盘问题 - [经典DFS]

    题目链接:http://poj.org/problem?id=1321 Time Limit: 1000MS Memory Limit: 10000K Description 在一个给定形状的棋盘(形 ...

随机推荐

  1. [WIFI] WIFI 破解(初级)

    话不多说,先来看看字典破解 wpa2 的效果 =================================== ========================================= ...

  2. EDT改成CST

    功能说明:显示文字.语 法:echo [-ne][字符串]或 echo [--help][--version]补充说明:echo会将输入的字符串送往标准输出.输出的字符串间以空白字符隔开, 并在最后加 ...

  3. SpringCloud--Ribbon负载均衡

    Ribbon实现客户端负载均衡 负载均衡:是对系统的高可用.网络压力的缓解和处理能力扩容的重要手段之一. 硬件负载均衡:主要通过在服务器节点之间安装专门用于负载均衡的设备: 软件负载均衡:通过在服务器 ...

  4. Oracle下Insert的介绍

    Insert是插入语句 insert into table(colname1,colname2) values(value1,valu2) 插入无效的会提示失败 数值类型在插入的时候不需要加引号,但是 ...

  5. vue - 指令系统

    指令系统: 所谓指令系统,大家可以联想咱们的cmd命令行工具,只要我输入一条正确的指令,系统就开始干活了. 在vue中,指令系统,设置一些命令之后,来操作我们的数据属性,并展示到我们的DOM上. 1. ...

  6. laydate设置起始时间,laydate设置开始时间和结束时间

    //设置开始时间 var startDate = laydate.render({ elem: '#start_date',//开始时间选择控件id min:'2018-6-1', type: 'da ...

  7. qt——类大全

    qt类总结地址 http://www.kuqin.com/qtdocument/ QWidget.QDialog及QMainWindow的区别 QWidget类是所有用户界面对象的基类. 窗口部件是用 ...

  8. (2.3)DDL增强功能-流程化控制与动态sql

    1.流程控制 在T-SQL中,与流程控制语句相关的关键字有8个: BEGIN...END BREAK GOTO CONTINUE IF...ELSE WHILE RETURN WAITFOR 其实还可 ...

  9. mysql 大表优化

    当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 转自:https://segmentfault.com/a/1190000006158186 单表优化 除非单表数据未 ...

  10. 基于struts2框架-自定义身份证号验证器

    自定义拦截器的步骤: 1.定义一个验证器的类: > 自定义的验证器都需要实现 Validator接口.  > 可以选择继承 ValidatorSupport 或 FieldValidato ...