Locker
Time Limit: 6000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 890 accepted submission (s): 380
Problem descriptiona 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?
Inputmultiple (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.
Outputfor each case, output one integer, the minimum amount of steps from the current state to the secret password.
Sample Input
111111 222222896521 183995
Sample output
212
Source2012 Asia Tianjin Regional Contest
Recommendzhoujiaqi2010
Question:
Two strings are given. You can select 1-3 consecutive numbers each time, add 1 at the same time or subtract 1 at the same time, and ask how many operations are performed to convert one string into another.
Ideas:
DP [I] [J] [k] indicates that the first I has been completely matched. At this time, the first I + 1 has already added J, and the second I + has added K
Transfer is divided into two steps: enumeration plus, enumeration Subtraction
How can I transfer from DP [I] to DP [I + 1?
DP [I] indicates that the I position has been adjusted. We want to adjust the I + 1 position. There are three ways to change the I position.
1. Change the I + 1 position separately.
2. Change the positions I + 1 and I + 2 at the same time.
3. Change the positions I + 1, I + 2, And I + 3 at the same time.
So for each change.If I + 1 Changes A, I + 2 Changes B, And I + 3 changes C, A> = B> = C.
Because three of the three methods can change I + 1. The two can change I + 2. A change to I + 3.
For details, seeCode:
# Include <stdio. h> # include <iostream> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 1010; char S1 [maxn], S2 [maxn]; int DP [maxn] [10] [10]; int main () {int I, J, K, X, Y, T, Len, up, DW; while (~ Scanf ("% S % s", S1, S2) {Len = strlen (S1); memset (DP, 0x3f, sizeof DP ); DP [0] [0] [0] = 0; for (I = 0; I <Len; I ++) {for (j = 0; j <10; j ++) {up = (s2 [I]-S1 [I]-J + 20) % 10; // adjust the number of steps to be added to the I + 1 position DW = (10-up) % 10; // You Need to subtract down for (k = 0; k <10; k ++) {for (x = 0; x <= up; X ++) // enumerated I + 2 bits can be added {T = (k + x) % 10; // After I + 1 is adjusted, I + 2 bits have been added for (y = 0; y <= x; y ++) // enumerate values that can be added with I + 3 bits DP [I + 1] [T] [Y] = min (DP [I + 1] [T] [Y], DP [I] [J] [k] + up);} For (x = 0; x <= dw; X ++) // enumerate the values that can be reduced by I + 2 Characters {T = (k-x + 10) % 10; // After I + 1 is adjusted, I + 2 bits have been added for (y = 0; y <= x; y ++) // enumerate the values that can be reduced by I + 3 bits DP [I + 1] [T] [(10-y) % 10] = min (DP [I + 1] [T] [(10-y) % 10], DP [I] [J] [k] + DW) ;}}} printf ("% d \ n ", DP [Len] [0] [0]);}