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. Java初学者笔记一:元类、获取类型、枚举

    零.绪论: 2018年新年伊始,学习Java的冲动越来越强烈,毕竟以后无论是做安全开发还是安全研究都必不可少的掌握这门语言,所以在不断完善Python作为脚本语言的主语言的情况下觉得学习Java作为高 ...

  2. Weinre 远程调试移动端手机web页面

    调试场景 1.调试页面在手机上.2.调试工具在PC的chrome3.手机跟pc要在同一个网络环境下,也就是都使用一个wifi 一.安装 Weinre 1.Weinre是基于nodejs实现的,所以使用 ...

  3. ipmi监控主机

    author: headsen   chen date: 2018-09-25  20:04:01 IPMI介绍       IPMI(Intelligent Platform Management ...

  4. 【BZOJ5101】[POI2018]Powód 并查集

    [BZOJ5101][POI2018]Powód Description 在地面上有一个水箱,它的俯视图被划分成了n行m列个方格,相邻两个方格之间有一堵厚度可以忽略不计的墙,水箱与外界之间有一堵高度无 ...

  5. 【BZOJ3551】[ONTAK2010]Peaks加强版 最小生成树+DFS序+主席树

    [BZOJ3545][ONTAK2010]Peaks Description 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困 ...

  6. @Apiimplicitparam的paramType

    转自:https://swagger.io/docs/specification/describing-parameters/ Parameter Types OpenAPI 3.0 distingu ...

  7. OC开发_Storyboard——Core Data

    一 .NSManagedObjectContext 1.我们要想操作Core Data,首先需要一个NSManagedObjectContext2.那我们如何获得Context呢:创建一个UIMana ...

  8. mysql常见的错误码

    Mysql错误代码 Mysql错误代码分为两部分,老版本一部分,4.1版本为新的部分 第一部分: mysql的出错代码表,根据mysql的头文件mysql/include/mysqld_error.h ...

  9. [Nginx] – 安全优化 – 配置文件优化

    1.配置Nginx gzip压缩实现性能优化 1.Nginx gzip压缩功能介绍  Nginx gzip压缩模块提供了压缩文件内容的功能,用户请求的内容在发送出用客户端之前,Nginx服务器会根据一 ...

  10. Eclipse git pull 报Nothing to fetch 异常原因

    eclipse git pull 报错 // 使用这个配置就可以正常pull了        [core]        symlinks = false         repositoryform ...