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 ...
随机推荐
- java并发面试题-基础
多线程 java中有几种方法可以实现一个线程? 1.直接继承thread类:2.实现runnable接口: 如何停止一个正在运行的线程?可以使用正在运行的线程,支持线程中断,通常是定义一个volati ...
- HDU 2177 取(2堆)石子游戏
取(2堆)石子游戏 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- win8 远程桌面时提示凭证不工作问题的终极解决办法
环境说明 远程办公电脑(放置于公司.自用办公电脑.win8系统) 远程连接客户机(放置于家中.家庭日常所用.win8系统) 故障现象 最近在使用远程桌面连接公司的办公电脑时,突然发现win8系统总是无 ...
- 使用 SpiritManager 类管理在 XNA 游戏中的精灵(十四)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- MongoDB用PCRE正则表达式
介绍 下面说明 PCRE 所支持的正则表达式的语法和语义.Perl 文档和很多其它书中也解说了正则表达式,有的书中有很多例子.Jeffrey Friedl 写的“Mastering Regular E ...
- Python+Selenium练习篇之14-获取当前页面的title
前面文章介绍了如何获取当前页面的URL的值,本文介绍如何获取当前页面的title,这个也可以作为测试结果的依据,通过得到的title和预期的值对比,可以支持我们判断页面跳转正确. 相关脚本代码如下: ...
- CSU-2049 象棋
CSU-2049 象棋 Description 車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子.一天,小度在棋盘上摆起了许多車--他想知道,在一共N×M个点的矩形棋盘中摆最多 ...
- HDU 2440、HDU 3694多边形费马点
1.http://acm.hdu.edu.cn/showproblem.php?pid=2440 按照题意知道是一个简单的多边形即凸包,但给出的点并没有按照顺序的,所以需要自己先求出凸包,然后在用 ...
- iOS中常见的自定义宏
//字符串是否为空 #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str leng ...
- 【bzoj2738】矩阵乘法 整体二分+二维树状数组
题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入 第一行两个数N,Q,表示矩阵大小和询问组数:接下来N行N列一共N*N个数,表示这个矩阵:再接下来Q行每行5个数 ...