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. 用C或C++为Python编写模块

    1.使用c或c++编写对应的函数例如: //modtest.c int abs(int number){ ){ return -number; } else{ return number; } } 2 ...

  2. Egret P2 入门学习资料

    1 p2库下载: https://github.com/egret-labs/egret-game-library/tree/rc/4.1.0 2 p2 作者demo:https://github.c ...

  3. SSH电力项目四-显示首页

    1.登录页面: 将上一节中的页面放到/WEB-INF/page/目录下,需要登录后才能访问该页面: 对应页面:/WEB-INF/page/menu/home.jsp <%@ page langu ...

  4. maven指定项目的构建、打包和tomcat插件的pom.xml配置

    1.pom.xml添加如下配置: <build> <finalName>${parent.artifactId}</finalName> <plugins&g ...

  5. flex弹性布局属性详解!

    详细看下flex弹性布局具体属性: flex容器属性详解:flex-direction:row/column:(横排/竖排) 决定元素的排列方向:flex-wrap:nowrap/wrap/wrap- ...

  6. CHECKSUM比较两表字段值差异

    CHECKSUM 返回在表的行上或在表达式列表上计算的校验值.CHECKSUM 用于生成哈希索引. 语法 CHECKSUM ( * | expression [ ,...n ] ) 参数 * 指定在表 ...

  7. pycharm 和 Anaconda 下的 opencv 安装

    学习真的切忌三天打鱼两天晒网!! 一开始python下的opencv已经都弄好了,中间电脑坏了一次,好久没有接触这个,就全部都忘完了.深感惋惜. 今天又从新安装了一下opencv.在anaconda下 ...

  8. [Gradle] 获取 gradle 命令行参数

    project.gradle.startParameter 参考 StartParameter | Gradle API 4.9

  9. Gitlab安装和使用

     GitLab是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目.        GitLab拥有与Github类似 ...

  10. CentOS7使用yum安装nginx

    CentOS默认没有nginx的yum源需要yum安装nginx可以使用一下方法 一,环境检测 二,设置yum源 rpm -Uvh http://nginx.org/packages/centos/7 ...