P1279 String distance

Topic description

Given a string X, we call the new string formed by inserting any number of spaces at the head, tail and middle of X as an extended string of X, if the string X is "abcbcd", then the string "abcb cd", Both "□a□bcbcd□" and "abcb□cd□" are extended strings of X, where "□" represents the space character.

If A1 is an extension of string A, B1 is an extension of string B, and A1 and B1 have the same length, then I define the distance between strings A1 and B1 as the sum of the distances of the characters in the corresponding positions, and the two The distance of a non-space character is defined as the absolute value of the difference of their ASCII codes, and the distance between a space character and any other character is a known fixed value K, and the distance between a space character and a space character is 0. In all extension strings of strings A and B, there must be two extension strings A1 and B1 of equal length, so that the distance between A1 and B1 is minimized. We define this distance as the distance between strings A and B. .

Please write a program to find the distance between strings A and B.

Input and output format

Input format:

 

The first line of the input file is string A, and the second line is string B. Both A and B consist of lowercase letters and the length should not exceed 2000. The third line is an integer K (1≤K≤100), indicating the distance between spaces and other characters.

 

Output format:

 

Only one line of the output file contains an integer, which represents the distance between the obtained strings A and B.

 

Input and output example

Input example #1: 
cc
snmn
2
Sample output #1: 
  10
 

Solution

  Obviously I still lack some ideas. I only want to come up with half of this DP. Fortunately, I read the solution later, but the idea is the same. It is that I have some problems with preprocessing.

  As a DP question, this question is actually not difficult.

 

 We take f[i][j] as the state, which is the optimal solution for the ith bit of the first string and the jth bit of the second string.

  After reading the question, you can know that there are only two cases of characters in each position:

  1. The ith character in the current first string matches a space.

  2. The i-th character in the current first string matches the j-th character in the second string.

 

  That is, each f[i][j] has three leading states:

  1. f [ i-1 ] [ j ] now means that the ith of the first string matches a space.

  2. f[i][j-1] is now the jth of the second string that matches a space.

  3. f [ i-1 ] [ j-1 ] At this time, the current two characters i and j can be matched.

  So the dynamic transfer equation can be deduced.

  What needs to be paid attention to is preprocessing. It is necessary to take into account the matching status of each character and an empty string.

 

  code

   

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int f[2008][2008];
 4 char ch1[2008],ch2[2008];
 5 int len1,len2,kk;
 6 int main()
 7 {
 8     scanf("%s",ch1);
 9     scanf("%s",ch2);
10     cin>>kk;
11     len1=strlen(ch1);
12     len2=strlen(ch2);
13     for(int i=1;i<=len1;i++) f[i][0]=f[i-1][0]+kk;
14     for(int i=1;i<=len2;i++) f[0][i]=f[0][i-1]+kk;
     //预处理部分需要注意
15 for(int i=1;i<=len1;i++) 16 for(int j=1;j<=len2;j++) 17 { 18 f[i][j]=min(min(f[i-1][j]+kk,f[i][j-1]+kk),f[i-1][j-1]+abs(ch1[i-1]-ch2[j-1])); 19 } 20 cout<<f[len1][len2]<<endl; 21 return 0; 22 }

 

 

   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324460361&siteId=291194637