Reprinted please indicate the source, thank youHttp://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove
Question: two strings are given. You can select 1-3 consecutive numbers, add 1 at the same time or subtract 1 at the same time, and ask the minimum number of operations to convert one string to another.
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4433
BFS can have similar questions in the past.
However, the data volume of this question is too large, and many of them have been searched.
I wrote a search for Objective B, but it was suspended and no optimization was added.
During the training, the DP Solution of yobobo was very similar, but it seems to be stuck in the Post-efficiency?
DP [I] [J] [k] indicates that the first I has been completely matched. At this time, the I + 1 has been added with J bits, K has been added for the I + 2 bits.
Transfer is divided into two steps: enumeration plus, enumeration Subtraction
Note: If the I-th vertex adds a, the I + 1-th vertex adds B, and the I + 2-th vertex adds C, the relation A> = B> = C cannot be wrong.
# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # define INF 1 <20 # define n 1005 using namespace STD; char S1 [N], S2 [N]; int DP [N] [10] [10]; int main () {While (scanf ("% S % s ", s1, S2 )! = EOF) {int L = strlen (S1); For (INT I = 0; I <= L; I ++) for (Int J = 0; j <10; j ++) for (int K = 0; k <10; k ++) DP [I] [J] [k] = inf; DP [0] [0] [0] = 0; For (INT I = 0; I <L; I ++) for (Int J = 0; j <10; j ++) for (int K = 0; k <10; k ++) {int t = (s2 [I]-S1 [I]-J + 20) % 10; for (int A = 0; A <= T; A ++) for (INT B = 0; B <= A; B ++) DP [I + 1] [(K + a) % 10] [B] = min (DP [I + 1] [(K + a) % 10] [B], DP [I] [J] [k] + T); t = (10-t) % 10; for (int A = 0; A <= T; A ++) for (INT B = 0; B <= A; B ++) DP [I + 1] [(k-A + 10) % 10] [(10-b) % 10] = min (DP [I + 1] [(k-A + 10) % 10] [(10-b) % 10], DP [I] [J] [k] + T);} printf ("% d \ n", DP [l] [0] [0]);} return 0 ;}