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. Vue nodejs商城项目-商品列表页面组件

    data(){        return {            goodsList:[], // 商品列表            priceFilter:[ // 价格区间数组          ...

  2. Android 初步-Android文件目录介绍

    src:存放的是应用程序使用到的java文件. gen:系统自动生成的目录,不需要程序员进行修改,包含了R.java文件.该文件包含了 程序使用到的资源文件对应的唯一资源ID,注意:如果R文件生成错误 ...

  3. spring入门(六) spring mvc+mybatis

    1.引入依赖 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> < ...

  4. Swift_数组详解

    Swift_数组详解 点击查看源码 初始化 //初始化 fileprivate func testInit() { //空数组 var array = [Int]() print(array) arr ...

  5. Angularjs基础(四)

    AngularJS过滤器 过滤器可以使用一个管道符(|)添加到表达式和指令中. AngularJS过滤器可用于转换数据: currency 格式化数字为货币格式 filter 从数组中选着应子集. l ...

  6. poj 1236 Network of Schools : 求需要添加多少条边成为强连通图 tarjan O(E)

    /** problem: http://poj.org/problem?id=1236 缩点后入度为0的点的总数为需要发放软件的学校个数 缩点后出度为0的点的总数和入度为0的点的总数的最大值为需要增加 ...

  7. 访问本地方站出现EOF的分析和解决

    每天早晨打开电脑运行本地项目的时候,有时候浏览器上会出现EOF 之前都都能正常访问,所以我猜想本地的项目本身肯定是没有问题的. Google了下,发现有人说是代理的问题,于是关闭代理试过后,发现可以访 ...

  8. github 常用

    1.创建KEY,这个文件生成完了后,要保存好公钥和私钥文件 ssh-keygen -t rsa -C "abc@mail.com" 2.github上添加ssh密钥 3.拷贝公钥信 ...

  9. Docker(三):部署软件

    Docker的镜像文件可以在镜像仓库中进行搜索. 部署软件目录导航: 常用命令 部署 Tomcat 部署 MySQL 部署 Oracle 常用命令 docker的常用命令如下: docker -v , ...

  10. 虚拟环境管理之virtualenvwrapper

    上一篇写了下在linux上使用python的虚拟环境, 干脆把virtualenvwrapper也写一下 1.为什么要用virtualenvwrapper virtualenv 的一个最大的缺点就是: ...