Problem Description
A password locker with N digits, each digit can be rotated to 0-9 circularly.
You can rotate 1-3 consecutive digits up or down in one step.
For examples:
567890 -> 567901 (by rotating the last 3 digits up)
000000 -> 000900 (by rotating the 4th digit down)
Given the current state and the secret password, what is the minimum amount of steps you have to rotate the locker in order to get from current state to the secret password?
 
Input
Multiple (less than 50) cases, process to EOF.
For each case, two strings with equal length (≤ 1000) consists of only digits are given, representing the current state and the secret password, respectively.
 
Output
For each case, output one integer, the minimum amount of steps from the current state to the secret password.

题目大意:从一串数字转成另一串数字,每次操作可以把连续的1~3个数字都+1或者-1(9+1=0,0-1=9),问最少须要多少次操作。

思路:DP。dp[i][x][y]表示,把第一个字符串变为第 i 位是 x,第 i-1 位是 y, i-1前面的全部都与第二个字符串相同,最少须要多少步。

假设第一串数字是a[],第二串数字是b[]

那么状态转移就是,对于每一个dp[i][x][y],判断从a[i]须要转多少步操作才能到x(两种操作都要枚举),设为k,然后枚举 i-1 跟着转多少步才能到达 y,设为p(p≤k),再枚举 i-2 须要跟着转多少步才能到达b[i],设为q(q≤p)。详见代码。

最终答案为dp[n][b[n]][b[n-1]]

代码(203MS):

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL; const int MAXN = ; int a[MAXN], b[MAXN], n;
int dp[MAXN][][];
char s[MAXN]; inline int make(int x) {
if(x < ) x += ;
if(x > ) x -= ;
return x;
} inline void update_min(int &a, const int &b) {
if(a > b) a = b;
} inline int calc(int x, int y) {
if(x > y) y += ;
return y - x;
} int solve() {
memset(dp, 0x3f, sizeof(dp));
dp[][a[]][] = ;
for(int i = ; i <= ; ++i) {
update_min(dp[][make(a[] + i)][], i);
update_min(dp[][make(a[] - i)][], i);
}
for(int i = ; i <= n; ++i) {
for(int x = ; x <= ; ++x) {
for(int y = ; y <= ; ++y) {
int k = calc(a[i], x);
for(int p = ; p <= k; ++p) {
for(int q = ; q <= p; ++q)
update_min(dp[i][x][y], dp[i - ][make(y - p)][make(b[i - ] - q)] + k);
} k = - k;
for(int p = ; p <= k; ++p) {
for(int q = ; q <= p; ++q)
update_min(dp[i][x][y], dp[i - ][make(y + p)][make(b[i - ] + q)] + k);
}
}
}
}
return dp[n][b[n]][b[n - ]];
} int main() {
while(scanf("%s", s) != EOF) {
n = strlen(s);
for(int i = ; i < n; ++i) a[i + ] = s[i] - '';
scanf("%s", s);
for(int i = ; i < n; ++i) b[i + ] = s[i] - '';
printf("%d\n", solve());
}
}

HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)的更多相关文章

  1. HDU-4432-Sum of divisors ( 2012 Asia Tianjin Regional Contest )

    Sum of divisors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU 4441 Queue Sequence(优先队列+Treap树)(2012 Asia Tianjin Regional Contest)

    Problem Description There's a queue obeying the first in first out rule. Each time you can either pu ...

  3. HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)

    Problem Description In this problem, you are given several strings that contain only digits from '0' ...

  4. HDU 4431 Mahjong(枚举+模拟)(2012 Asia Tianjin Regional Contest)

    Problem Description Japanese Mahjong is a four-player game. The game needs four people to sit around ...

  5. HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)

    Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...

  6. HDU 4433 locker 2012 Asia Tianjin Regional Contest 减少国家DP

    意甲冠军:给定的长度可达1000数的顺序,图像password像锁.可以上下滑动,同时会0-9周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...

  7. HDU 4468 Spy(KMP+贪心)(2012 Asia Chengdu Regional Contest)

    Description “Be subtle! Be subtle! And use your spies for every kind of business. ”― Sun Tzu“A spy w ...

  8. HDU 3726 Graph and Queries(平衡二叉树)(2010 Asia Tianjin Regional Contest)

    Description You are given an undirected graph with N vertexes and M edges. Every vertex in this grap ...

  9. HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)

    Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...

随机推荐

  1. 大专生自学web前端前前后后

    先做个自我介绍,我13年考上一所很烂专科民办的学校,学的是生物专业,具体的学校名称我就不说出来献丑了.13年我就辍学了,我在那样的学校,一年学费要1万多,但是根本没有人学习,我实在看不到希望,我就退学 ...

  2. 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/ 本文出自方志朋的博客 一.spring ...

  3. SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates访问Mysql

    转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/springboot3-JdbcTemplates-Mysql/ 本文出 ...

  4. 开发工具--Eclipse使用及常见问题解决

    怎么查询Eclipse版本号: 方法一: 方法二: Eclipse安装目录下面找到readme文件夹,里边有个网页打开就可以看到当前版本; Eclipse汉化改为英文: Eclipse Mybatis ...

  5. linux各种抓包情况说明

    大家都知道抓包指令:tcpdump    抓包的主要目的是测试端口.网络协议通不通,以及对抓取的数据包进行分析.测试,抓包对熟悉linux的大神都不陌生,网络对于我来说也是一窍不通,只是在这里记录一下 ...

  6. 嵌入式:FreeRTOS的使用(未完)

    为了方便与UCOS对比,顺序按照UCOS那篇编写. 0.一些移植.系统相关 1.框架写法(个人习惯相关) 1-1.main 函数里创建一个开始任务 int main(void) { 初始化外设 xTa ...

  7. 重写Alert和confirm方法去除地址显示

    //重写alert方法,去掉地址显示window.alert = function(name){var iframe = document.createElement("IFRAME&quo ...

  8. java程序员所必须掌握的内容

    以下内容有待细化,并用于考察员工的水平! 从低的往高级的说. 初级 1.掌握java语法 oop+io+网络 2.基本的数据结构 3.基本的算法-例如排序,合并 4.基本的几个javaee框架 spr ...

  9. FROM_UNIXTIME

    FROM_UNIXTIME 格式化MYSQL时间戳函数   函数:FROM_UNIXTIME作用:将MYSQL中以INT(11)存储的时间以"YYYY-MM-DD"格式来显示.语法 ...

  10. Centos安装docker#避免很多坑

    采用yum方式安装 安装: step 1: 安装必要的一些系统工具 yum install -y yum-utils device-mapper-persistent-data lvm2 Step 2 ...