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. 上传文件,经过Zuul,中文文件名乱码解决办法

    转载请标明出处: http://blog.csdn.net/forezp/article/details/77170470 本文出自方志朋的博客 问题描述 在项目中又一个上传文件的oss服务,直接调用 ...

  2. Struts2 第四讲 -- Struts2的基本配置

    5.struts2的基本配置 5.1 struts2的访问连接url 在struts1中,通过<action path=“/primer/helloWorldAction.action”> ...

  3. pl sql 存储过程、函数

    存储过程用于执行特定的操作,当建立存储过程时,既可以指定输入参数(in),也可以指定输出参数(out),通过在过程中使用输入参数,可以将数据传递到执行部分:通过使用输出参数,可以将执行部分的数据传递到 ...

  4. 转:Spring Boot应用中的异常处理

    引自:https://www.cnblogs.com/yangfanexp/p/7616570.html 楼主前几天写了一篇“Java子线程中的异常处理(通用)”文章,介绍了在多线程环境下3种通用的异 ...

  5. JS高级. 02 面向对象、创建对象、构造函数、自定义构造函数、原型

    面向对象的三大特性: 封装 a)  把一些属性和方法装到一个对象里 2.  继承 a)  js中的继承是指:   一个对象没有一些方法和属性,而另一个对象有 把另一个个对象的属性和方法,拿过来自己用, ...

  6. 使用docker安装和运行常用的数据库和中间件

    mysql: docker pull mysql: docker run --name mysql -p : -v /usr/share/zoneinfo/Asia/Shanghai:/etc/loc ...

  7. C语言关于指针的注意事项

    一.指针的四个关键概念1.指针的类型2.指针指向的类型3.指针的值,也就是指针指向的地址4.指针自己所占用的内存空间注意:指针变量所存的内容就是内存的地址编号! 例如:int **pp = NULL; ...

  8. video.js使用技巧

    https://www.awaimai.com/2053.html https://www.jianshu.com/p/16fa00a1ca8e

  9. 如何用Python做自动化特征工程

    机器学习的模型训练越来越自动化,但特征工程还是一个漫长的手动过程,依赖于专业的领域知识,直觉和数据处理.而特征选取恰恰是机器学习重要的先期步骤,虽然不如模型训练那样能产生直接可用的结果.本文作者将使用 ...

  10. crest value &minimum

    public class paixu { public static void main(String[] args) { double temp; double num[]={5.1, 7.12, ...