NEUOJ 1262: ASCII Sequence II
Description
Given two number string of the same length, we assume their distance is the sum of every number at the same position difference. For example the difference between “123” and “321” is |3-1|+|2-2|+|1-3| =4.
So our question is that: given two number string sequence s1,s2 (the length of s2 is smaller) please find the subsequence of s1, for it is nearest from s2.
Input
There are several test cases,every test case have number sequence s1,s2.all char is from 0 to 9 ,And the length is smaller than 1000.
Output
Please output the minimum distance from the s1 subsequence to s2.
Sample Input
123
321
123456789
13579
321
123456789
13579
Sample Output
4
0
东软杯的动规题之一,出乎意料的大水题。。。状态转移方程非常好写,需要注意的是不是所有的空间都用
的上,有些位置是肯定无法匹配的,还有一个困扰了我很久的问题就是和最小值究竟要怎么表示。。本来
想咨询一下陶大神的。。。结果一问完突然之间就灵光一现。。想到了解法:注意到abs(a[i]-b[j])的值
永远小于10,故可以用10-abs(a[i]-b[j]) 把这个问题转化成和的最大值。。
但是我总感觉这个方法有点取巧的嫌疑,所以要是有更好的方法请不吝赐教啊。。。
0
东软杯的动规题之一,出乎意料的大水题。。。状态转移方程非常好写,需要注意的是不是所有的空间都用
的上,有些位置是肯定无法匹配的,还有一个困扰了我很久的问题就是和最小值究竟要怎么表示。。本来
想咨询一下陶大神的。。。结果一问完突然之间就灵光一现。。想到了解法:注意到abs(a[i]-b[j])的值
永远小于10,故可以用10-abs(a[i]-b[j]) 把这个问题转化成和的最大值。。
#include <cstdio>
#include <cstring>
#include <algorithm>
using
namespace
std;
int
d[1001][1001];
int
main()
{
char
a[1001], b[1001];
while
(
scanf
(
"%s%s"
, a, b) == 2)
{
int
alen =
strlen
(a), blen =
strlen
(b);
for
(
int
i=1; i<=blen; i++)
{
for
(
int
j=i; j<=alen-blen+i; j++)
{
d[i][j] = max(d[i][j-1], d[i-1][j-1]+10-
abs
(a[j-1]-b[i-1]));
}
}
printf
(
"%d\n"
, 10*blen-d[blen][alen]);
}
return
0;
}
但是我总感觉这个方法有点取巧的嫌疑,所以要是有更好的方法请不吝赐教啊。。。