spoj 2325 String Distance(滚动数组)

String Distance


Problem code: STRDIST


Let A = a1a2...ak and B = b1b2...bl be strings of lengths k and l, respectively. The string distance between A and B is defined in the following way (d[i,j] is the distance of substrings a1...ai and b1...bj, where 0 ≤ i ≤ k and 0 ≤ j ≤ l -- i or j being 0 represents the empty substring). The definition for d[i, j] is d[0, 0] = 0 and for (i, j) ≠ (0, 0) d[i, j] is the minimum of all that apply:


d[i, j - 1] + 1, if j > 0
d[i - 1, j] + 1, if i > 0
d[i - 1, j - 1], if i > 0, j > 0, and ai = bj
d[i - 1, j - 1] + 1, if i > 0, j > 0, and ai ≠ bj
d[i - 2, j - 2] + 1, if i ≥ 2, j ≥ 2, ai = bj-1, and ai-1 = bj
The distance between A and B is equal to d[k,l].


For two given strings A and B, compute their distance knowing that it is not higher than 100.


Input


In the first line, k and l are given, giving the lengths of the strings A and B (1 ≤ k, l ≤ 105). In the second and third lines strings A and B, respectively, are given. A and B contain only lowercase letters of the English alphabet.


Output


In the first line, write one number, the distance between A and B, followed by a newline.


Example


Input:
8 8
computer
kmpjutre


Output:

4


题意:求两个字符串之间的距离,距离定义为d[i, j - 1] + 1, if j > 0
d[i - 1, j] + 1, if i > 0
d[i - 1, j - 1], if i > 0, j > 0, and ai = bj
d[i - 1, j - 1] + 1, if i > 0, j > 0, and ai ≠ bj
d[i - 2, j - 2] + 1, if i ≥ 2, j ≥ 2, ai = bj-1, and ai-1 = bj
The distance between A and B is equal to d[k,l].

思路:两字符串的长度都可以到10^5,用dp[i][j]表示两字符串之间的距离,因为dp[i][]只和dp[i-1]或dp[i-2][]有关,我们可以使用滚动数组来节省内存空间。


AC代码:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
    //freopen("1.txt","r",stdin);
    int dp[3][100005];
    int k,l;
    char a[100005],b[100005];
    scanf("%d%d",&k,&l);
    if(k!=0)
        scanf("%s",a+1);
    if(l!=0)
        scanf("%s",b+1);
    if(k==0&&l==0)
        printf("0\n");
    else if(k==0&&l!=0)
        printf("%d\n",l);
    else if(k!=0&&l==0)
        printf("%d\n",k);
    else
    {
        dp[0][0]=0;
        for(int i=0; i<=k; i++)
        {
            int j=0;
            if(i>100) j=i-100;
            for(; j<=i+100&&j<=l; j++)
            {
                if(i==0&&j==0) continue;
                int temp=100000000;
                if(j>0)
                    temp=min(temp,dp[i%3][j-1]+1);

                if(i>0)
                    temp=min(temp,dp[(i-1)%3][j]+1);

                if(i>0&&j>0&&a[i]==b[j])
                    temp=min(temp,dp[(i-1)%3][j-1]);

                else if(i>0&&j>0&&a[i]!=b[j])
                    temp=min(temp,dp[(i-1)%3][j-1]+1);

                if(i>=2&&j>=2&&a[i]==b[j-1]&&b[j]==a[i-1])
                    temp=min(temp,dp[(i-2)%3][j-2]+1);
                dp[i%3][j]=temp;
            }
        }

        printf("%d\n",dp[k%3][l]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值