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 ...
随机推荐
- WPF异步回调时回调函数如何获取异步函数产生的变量
有这么一个问题,WPF在使用异步回调的时候,回调函数需要用到异步函数里产生的一个变量,例如异步函数里查询数据库得到了一个DataTable,如何传递给回调函数呢? [方案一]使用全局变量 很容易想到的 ...
- BZOJ 1222: [HNOI2001]产品加工
F[i]表示第一个机器用了i的时间,第二个机器的最小时间 转移即可 #include<cstdio> #include<algorithm> using namespace s ...
- vrpie下实现vrp模型和javascript的交互
最近在做一个vrpie的项目,用vrp建模生成vrpie,然后在网页上面显示,这里需要和网页上面的其他内容交互,现在总结一下开发经验. 第一个需求是在网页上面点击那个的时候做一些事情,通过查找sdk找 ...
- IOS开发学习笔记010-面向对象的三大特性
面向对象的三大特性 1.封装 2.继承 3.多态 一.封装 将类内部的属性保护起来,在外部不能直接访问,那么如果需要访问怎么办呢? OC提供了set方法来对成员变量进行访问 set方法 1.作用:提供 ...
- 使用 Anime 类在 XNA 中创建小动画(十一)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- loadrunner11使用wplus_init_wsock录制非IE脚本/手机抓包
假如你的IE版本号太高,或者是chrome,firfox,又或者想录手机上的包,可以用loadrunner的wplus_init_wsock工具 1.在创建LR脚本的Start Recording ...
- var、let、const与JavaScript变量/常量的定义
早期的JavaScript中,声明变量只能使用var关键字定义变量,并没有定义常量的功能.通过var关键字定义的变量,其作用域只能函数级或是全局作用域,并没有块级作用域.ES6(ECMAScript ...
- POJ 3321:Apple Tree(dfs序+树状数组)
题目大意:对树进行m次操作,有两类操作,一种是改变一个点的权值(将0变为1,1变为0),另一种为查询以x为根节点的子树点权值之和,开始时所有点权值为1. 分析: 对树进行dfs,将树变为序列,记录每个 ...
- rmmod: can't change directory to “3.4.79+”,no such file or directory
直接在/lib/modules目录下,在该目录下建立3.4.79+这个文件夹即可
- html5 的 webScoket 和 C# 建立Socket连接
最近使用的web项目中,需要服务器直接触发前端显示效果. 所以研究了一下websocket: 名词解释: WebSocketWebSocket协议是一种双向通信协议,它建立在TCP之上,同http一样 ...