NEUOJ 1262: ASCII Sequence II

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

Sample Output

4
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;
}

但是我总感觉这个方法有点取巧的嫌疑,所以要是有更好的方法请不吝赐教啊。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值