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. 【BZOJ2946】[Poi2000]公共串 后缀数组+二分

    [BZOJ2946][Poi2000]公共串 Description        给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l        读入单词 l        计 ...

  2. HDCMS常用的一些调用!

    HDCMS常用的一些调用: 头部的标题/描述/关键词的调用: <title><?php if($hdcms['aid']):?><?php if($hdcms['seo_ ...

  3. Windows系统下做定时任务为Oracle数据库每天自动备份

    1.创建备份目录d:\backup, 创建批处理命令Bak.bat,编写备份脚本 ? 1 2 exp user/passwd@orcl DIRECT=Y BUFFER=100000 FILE=D:\b ...

  4. Docker企业级仓库Harbor的安装配置与使用

    Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全.标识和管理等,扩展了开源Docker Distribution.作为一个企业级 ...

  5. 比特币BTC全节点搭建

    比特币BTC全节点搭建 #环境 ubuntu 16.4 #硬盘500GB #截止2018-12-31磁盘占用超过230GB #客户端安装 #下载页面 #https://bitcoin.org/zh_C ...

  6. EOS Dapp开发(1)-基于Docker的开发环境搭建

    随着EOS主网的上线,相信基于EOS的Dapp开发会越来越多,查阅了很多资料相关的开发资料都不是很多,只能自己摸索,按照网上仅有的几篇教程,先git clonehttps://github.com/E ...

  7. 解决MySQL报错:1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'informat

    解决MySQL报错:1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'informat ...

  8. (转)SpringBoot非官方教程 | 第十二篇:springboot集成apidoc

    首先声明下,apidoc是基于注释来生成文档的,它不基于任何框架,而且支持大多数编程语言,为了springboot系列的完整性,所以标了个题. 一.apidoc简介 apidoc通过在你代码的注释来生 ...

  9. 5.6 Components -- Handling User Interaction with Actions

    1. 组件允许你定义可以在整个应用程序中重用的控件.如果它们够通用,它们也可以在被共享给其他人并且在许多应用程序中被使用. 2. 为了使一个可重用的控件有用,然而,你首先需要你的应用程序的用户和它交互 ...

  10. 142. Linked List Cycle II(找出链表相交的节点)

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...