sdut1650I-Keyboard(dp)
题目大意就相当于 跟你一串字符串 让你截成k段 使总体的值最小
想法是递归的 递归太慢 可以转换为递推的
这样就有可以推出状态方程 dp[i][j] = max(dp[i][j],dp[i-1][g]+sum[g+1][j]+sum[1][g]-sum[1][j]); dp[i][j]表示总长度为j第i次截的最小值 后面的sum[i][j]表示的就是从i开始作为第一个到j个的花费 o[i][j]保存路径
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<cmath>
#define INF 1e10
using namespace std;
char s[],ks[];
#define LL long long
LL dp[][],p[],sum[][];
int o[][],pa[];
int main()
{
//freopen("data1.in","r",stdin);
//freopen("text.out","w",stdout);
int t,k,i,j,l,g,kk=;
cin>>t;
while(t--)
{
memset(sum,,sizeof(sum));
kk++;
for(i = ; i < ; i++)
for(j = ; j < ; j++)
dp[i][j] = INF;
cin>>k>>l;
cin>>ks;
cin>>s;
for(i = ; i <= l ; i++)
cin>>p[i];
for(i = ; i <= l ; i++)
{
for(j = i; j <= l ; j++)
sum[i][j] = sum[i][j-]+p[j]*(j-i+);
}
for(i = ;i <= k- ; i++)
for(j = l ; j >= ; j--)
for(g = ; g < j ; g++)
{
if(dp[i][j]>dp[i-][g]+sum[][g]+sum[g+][j]-sum[][j])
{
dp[i][j] = dp[i-][g]+sum[][g]+sum[g+][j]-sum[][j];
o[i][j] = g;
}
}
int x = k-,y = l;
g = ;
while(x)
{
pa[++g] = o[x][y];
x--;y = pa[g];
}
printf("Keypad #%d:\n",kk);
pa[g+] = ;
for(i = g ;i >= ;i--)
{
printf("%c: ",ks[k-i-]);
for(j =pa[i+]+; j <= pa[i] ; j++)
{
printf("%c",s[j-]);
}
puts("");
}
printf("%c: ",ks[k-]);
for(j = pa[]+; j <= l ; j++)
printf("%c",s[j-]);
puts("");
puts("");
}
return ;
} /**************************************
Problem id : SDUT OJ 1650
User name : shang
Result : Accepted
Take Memory : 688K
Take Time : 200MS
Submit Time : 2014-02-15 11:17:10
**************************************/
sdut1650I-Keyboard(dp)的更多相关文章
- 【dp】New Keyboard
http://codeforces.com/gym/101397 B dp[i][j][k]: i为前一个行动的状态,0-switch.1-type,j为当前状态layout的编号,k 是已键入的字符 ...
- 【CF1238E】Keyboard Purchase(状压DP,贡献)
题意:有m种小写字符,给定一个长为n的序列,定义编辑距离为序列中相邻两个字母位置差的绝对值之和,其中字母位置是一个1到m的排列 安排一种方案,求编辑距离最小 n<=1e5,m<=20 思路 ...
- CF1238E.Keyboard Purchase 题解 状压/子集划分DP
作者:zifeiy 标签:状压DP,子集划分DP 题目链接:https://codeforces.com/contest/1238/problem/E 题目大意: 给你一个长度为 \(n(n \le ...
- Codeforces1238E. Keyboard Purchase(状压dp + 计算贡献)
题目链接:传送门 思路: 题目中的m为20,而不是26,显然在疯狂暗示要用状压来做. 考虑状压字母集合.如果想要保存字母集合中的各字母的顺序,那就和经典的n!的状态的状压没什么区别了,时间复杂度为O( ...
- USB Keyboard Recorder
catalogue . 引言 . Device Class Definition for Human Interface Devices (HID) . USB HID Report Descript ...
- HDU 3689 Infinite monkey theorem [KMP DP]
Infinite monkey theorem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- [LeetCode] 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
- [LeetCode] 2 Keys Keyboard 两键的键盘
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- 关于android屏幕适配的问题(drawable-xxxxxxxx,dp,sp,px等等),偶尔看到了android源代码,关于dpi的区分的值
上一篇博客说了一下.9.png图片http://blog.csdn.net/qq_23195583/article/details/46737419 当然,点九的是指的能够进行拉伸的.那么假设图片不能 ...
- hdu 3689 杭州 10 现场 J - Infinite monkey theorem 概率dp kmp 难度:1
J - Infinite monkey theorem Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
随机推荐
- easyUI 验证控件应用、自己定义、扩展验证 手机号码或电话话码格式
easyUI 验证控件应用.自己定义.扩展验证 手机号码或电话话码格式 在API中 发现给的demo 中没有这个验证,所以就研究了下. 相关介绍省略,直接上代码吧! watermark/2/tex ...
- 实习生面试相关-b
面试要准备什么 有一位小伙伴面试阿里被拒后,面试官给出了这样的评价:“……计算机基础,以及编程基础能力上都有所欠缺……”.但这种笼统的回答并非是我们希望的答案,所谓的基础到底指的是什么? 作为一名 i ...
- Atitit. 软件GUIbutton与仪表盘--webserver区--获取apache配置文件路径 linux and apache的启动、停止、重新启动
Atitit. 软件GUIbutton与仪表盘--webserver区--获取apache配置文件路径 linux and apache的启动.停止.重新启动 能够通过"netstat ...
- 积跬步,聚小流------Bootstrap学习记录(3)
响应式作为Bootstrap的一大特色.栅格系统可谓是功不可没,既然如此,那我们就来看一下栅格系统是怎样帮助bootstrap实现响应式布局的呢? 1.什么是栅格系统 我们能够从Bootstrap的官 ...
- mac WebStorm 破解
摘要:因为想要学习HTML所以需要一个工具,同事推荐了webstorm.下载以后再网上搜破解方法.搜索到一个很简单的. 一.下载链接https://www.jetbrains.com/webstorm ...
- WWDC笔记:2013 Session 203 What’s New in Cocoa Touch(未完)
Multitasking Background fetching New background mode fetch - (void)application:(UIApplication *)appl ...
- python 线程 进程 标识
s = '%s%s%s%s%s%s%s%s' % ( time.strftime('%Y%m%d %H:%M:%S', time.localtime(time.time())), ' os.getpp ...
- Effective C++学习笔记(Part Four:Item 18-25)
近期最终把effectvie C++细致的阅读了一边.非常惊叹C++的威力与魅力.近期会把近期的读书心得与读书笔记记于此,必备查找使用,假设总结有什么不 当之处,欢迎批评指正: 如今仅仅列出框架 ...
- LeetCode 7. Reverse Integer (JS)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- Linux网络协议栈(三)——网络设备(2)
2.1.网络设备的注册与注销注册网络设备发生在下列情形: (1)加载网卡驱动程序 网卡驱动程序如果被编译进内核,则它在启动时被初始化,在运行时被作为模块加载.无论初始化是否发生,所以由驱动程序控制 ...