POJ 1404 I-Keyboard (DP)
http://poj.org/problem?id=1404
题意 :手机上的要发短信的话,“我”字需要先按一下9键,再按3下6键,所以,现在想要重新布局每个键上的字母数,让最后的那个值最小,也就是说给你L个字母出现频率,让你将其分布在K个键上,字母顺序不可以变,每个字母出现的频率乘上他所在键的位置,再加总和最小即可。如果有多个解,尽量将字母往后边的键安排。
思路 :表示比赛的时候根本没看出这是DP,没反应过来,比完了才知道。。。这道DP不太好想,正好看了一个大神的博客,http://www.cnblogs.com/skyivben/archive/2011/11/09/2243068.html ,讲的很详细,可以参考一下。
#include <stdio.h>
#include <string.h>
#include <iostream> using namespace std ; int cost[][],price[][],indexx[][] ; void print(int K,int L,char key[],char let[])
{
if( K == ) return ;
print(K-, L-indexx[K][L],key,let) ;
printf("%c: ",key[K-]) ;
for(int i = L-indexx[K][L] ; i < L ; i++)
putchar(let[i]) ;
puts("") ;
}
int main()
{
int T ;
scanf("%d",&T) ;
int K,L ,a[] ,cnt = ;
while(T--)
{
char key[],let[] ;
cnt++ ;
scanf("%d %d %s %s",&K,&L,key,let) ;
for(int i = ; i < L ; i++)
scanf("%d",&a[i]) ;
memset(price,0x40,sizeof(price)) ;
price[][] = ;
for(int i = ; i <= L ; i++)
for(int j = i ; j <= L ; j++)
cost[i][j] = cost[i][j-]+(j-i+)*a[j-] ;
for(int i = ; i <= K ; i++)
for(int j = i ; j <= L ; j++)
for(int k = ; k <= j-i+ ; k++)
{
if(price[i-][j-k] + cost[j-k+][j] <= price[i][j])
price[i][j] = price[i-][j-k] + cost[j-k+][j],indexx[i][j] = k ;
}
printf("Keypad #%d:\n",cnt) ;
print(K,L,key,let) ;
printf("\n") ;
}
return ;
}
POJ 1404 I-Keyboard (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,开始自己没想到是区间 ...
- 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 ...
- poj 1463 Strategic game DP
题目地址:http://poj.org/problem?id=1463 题目: Strategic game Time Limit: 2000MS Memory Limit: 10000K Tot ...
- POJ 1947 (树形DP+背包)
题目链接: http://poj.org/problem?id=1947 题目大意:树中各点都由一条边连接.问要弄出个含有m个点的(子)树,至少需要截去多少条边. 解题思路: 设dp[i][j]为i总 ...
- [POJ 1155] TELE (树形dp)
题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...
- poj -2229 Sumsets (dp)
http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...
- poj 2688 状态压缩dp解tsp
题意: 裸的tsp. 分析: 用bfs求出随意两点之间的距离后能够暴搜也能够用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发訪问过s集合中的点.眼下在点u走过 ...
随机推荐
- MongoDB自定义函数部分 定义及引用
1. //定义一个Sum的函数 db.system.js.save({_id:"Sum", value:function(key,values) { ; ;i <values ...
- createjs 使用记录
createjs由几个库组成: l,easeljs,这个是核心,包括了显示列表.事件机制: 2,preloadjs,用于预加载图片等: 3,tweenjs,用于控制元件的缓动: 4,soundjs,用 ...
- 在页面生命周期执行时 Page 对象在 SaveState 阶段都发生了什么事?
先看下 SaveViewState 的源码: // Answer any state this control or its descendants want to save on freeze. / ...
- ### 学习《C++ Primer》- 8
Part 8: 面向对象(第15章) // @author: gr // @date: 2015-01-09 // @email: forgerui@gmail.com 一.OOP 面向对象程序设计的 ...
- JAVA时钟
效果图如下: //简单动态时钟程序,以图形和数字两种方式来显示当前时间 import javax.swing.*; import java.awt.event.*; import java.awt.* ...
- POJ3368(RMQ)
Frequent values Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreas ...
- OpenJudge / Poj 1044 Date bugs C++
链接地址: Poj:http://poj.org/problem?id=1044 OpenJudge:http://bailian.openjudge.cn/practice/1044/ 题目: 总时 ...
- [翻译][MVC 5 + EF 6] 8:更新相关数据
原文:Updating Related Data with the Entity Framework in an ASP.NET MVC Application 1.定制Course的Create和E ...
- ubuntu12.10设置禁止锁屏和屏幕常亮
1.System Settings -> Brightness and Lock -> Turn off screen... set to "Never" 进入ubun ...
- HDOJ(1000) A + B Problem
代码如下: #include <stdio.h> int main(void) { int a, b; ){ printf("%d\n", a+b); } ; }