POJ 1239 Increasing Sequences [DP]
题意:略。
思路:进行两次dp。
第一次dp从前向后,用dp[x]表示从第x位向前dp[x]位可构成一个数字,且与前面的数组符合题意要求。最后求的dp[n]即为最后一个数字的长度。
而题目还有要求,所有解中输出前面数字最大的一个。因此还需要进行一次dp,从后向前。
具体看代码吧,当初也是看别人代码才看懂的。
#include<stdio.h>
#include<string.h>
char num[];
int dp[], n;
bool judge(int st1,int len1,int st2,int len2)
{
while (num[st1] == '' && len1) st1++, len1--;
while (num[st2] == '' && len2) st2++, len2--;
if (len1 < len2) return ;
else if (len1 > len2) return ;
else
{
for (int i = ; i < len1; i++)
{
if (num[st1+i] < num[st2+i]) return ;
if (num[st1+i] > num[st2+i]) return ;
}
}
return ;
}
void print(int pos)
{
if (pos > n) return;
if (pos != ) printf(",");
for (int i = pos; i < pos + dp[pos]; i++)
printf("%c", num[i]);
print(pos + dp[pos]);
}
int main()
{
while (~scanf("%s", num + ) && strcmp(num + , ""))
{
n = strlen(num + );
dp[] = ;
for (int i = ; i <= n; i++)
{
dp[i] = i;
for (int j = i - ; j >= ; j--)
if (judge(j - dp[j] + , dp[j], j + , i - j))
{
dp[i] = i - j;
break;
}
}
int pos = n - dp[n] + ;
dp[pos] = dp[n];
for (int i = pos - ; i >= ; i--)
{
if (num[i] == '')
{
dp[i] = dp[i+] + ;
continue;
}
for (int j = pos; j > i; j--)
if (judge(i, j - i, j, dp[j]))
{
dp[i] = j - i;
break;
}
}
print();
printf("\n");
}
return ;
}
POJ 1239 Increasing Sequences [DP]的更多相关文章
- POJ 1239 Increasing Sequences 动态规划
题目链接: http://poj.org/problem?id=1239 Increasing Sequences Time Limit: 1000MSMemory Limit: 10000K 问题描 ...
- POJ 1239 Increasing Sequences(经典的两次dp)
http://poj.org/problem?id=1239 题意:给出一串序列,现在要添加逗号作为分隔符,使得序列是递增序列,然后让最后一个数尽量小,第一个数尽量大. 思路:先从头到尾进行一次dp, ...
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- TZOJ 5963 Increasing Sequences(线性DP)
描述 Given a string of digits, insert commas to create a sequence of strictly increasing numbers so as ...
- POJ 3107.Godfather 树形dp
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7536 Accepted: 2659 Descrip ...
- POJ 1179 - Polygon - [区间DP]
题目链接:http://poj.org/problem?id=1179 Time Limit: 1000MS Memory Limit: 10000K Description Polygon is a ...
- poj 3254 状压dp入门题
1.poj 3254 Corn Fields 状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...
- POJ 1260 Pearls 简单dp
1.POJ 1260 2.链接:http://poj.org/problem?id=1260 3.总结:不太懂dp,看了题解 http://www.cnblogs.com/lyy289065406/a ...
随机推荐
- 菜鸟学Linux - 设置文件/文件夹的权限
在Linux中,我们可以对文件或文件夹设置权限(r,w,x,-).然而,对文件和文件夹的权限设置,具有不同的意义.下面,通过几个例子来了解一下权限的意义所在.在开始之前,我们需要了解几个修改权限的命令 ...
- redis配置密码 redis常用命令
redis配置密码 1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 [plain] view plain copy #requi ...
- laravel5.2总结--blade模板
## 1.基本用法 ``` ##情形1 $name = laravel5 <div class="title"> {{$name}} {{$name}}</div ...
- 【Plus One】cpp
题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...
- Wordpress 作者模板页中的自定义帖子类型分页问题
<?php // 获取当前页面的页数,函数的参数为 paged $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $ ...
- 所有 Python 程序员必须要学会的「日志」记录。
本文字数:3840 字 阅读本文大概需要:10 分钟 写在之前 在我们的现实生活中,「日志记录」其实是一件非常重要的事情,比如银行的转账记录,汽车的行车记录仪记录行驶过程中的一切,如果出现了什么问题, ...
- Leetcode 581.最短无序连续子数组
最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, ...
- Android简单的BaseExpandableList使用
1.Activity package com.example.administrator.mystudent.ExpandableListView; import android.app.Expand ...
- 体验devstack安装openstack
由于公司制度,工作环境是不能直接上网的,所以在工作时间从没有体验过devstack或者其他联网方式安装openstack. 因自己购置了一台不错的主机,因而决定尝试安装一番,经过一段为期不短的内心极度 ...
- POJ 3254:Corn Fields(状态压缩DP)
题目大意:一个矩形的草地,分为多个格子,有的格子可以有奶牛(标为1),有的格子不可以放置奶牛(标为0),计算摆放奶牛的方案数. 分析: f[i,j]表示第i行状态为j的方案总数. 状态转移方程f[i, ...