There are given two strings, A and B. An expansion of some string X is a string created by adding or inserting any number (zero, one or more) of blanks anywhere in the string, or in the begining or the end of the string. Eg., if the string X is ‘abcbcd’, then the strings 'abcb-cd', '-a-bcbcd-' and 'abcb-cd-' are expansions of the string X (blanks are denoted by the character ‘-‘).
If A1 is an expansion of the string A, and B1 is and expansion of the string B, and if A1 and B1 are of the same length, then we define the distance of the strings A1 and B1 as the sum of the distances of the characters on the same positions in these strings. We define the distance of two characters as the absolute difference of their ASCII codes, except the distance of the blank and another character, which is given (and equal for all characters).
You are to write a program which finds the expansions A1 and B1 of strings A and B, that have the smallest difference.
Input
The first line of the input file consists of the string A, and the second line of string B. They are consisted only of lower case characters of the english alphabet (a-z), and the number of characters in any of the strings is less than or equal to 2000.
The third line consists of an integer K, 1 ≤ K ≤ 100, the distance of the blank and the other characters.
Output
The first only line of the input file should consist of the smallest distance as defined in the text of the task.
Sample
Input: cmc snmn 2 output: 10
Input: koiv ua 1 output: 5
input: mj jao 4 output: 12
题目大意:添加空格使得两个字符串的 距离值最小。
类似于 lcs 写法
题目中 k是 字符和空格之间的距离。 这里的‘距离’是题目中定义的
/* *********************************************** Author :guanjun Created Time :2016/9/4 18:45:31 File Name :1001.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 10010 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; priority_queue<int,vector<int>,greater<int> >pq; struct Node{ int x,y; }; struct cmp{ bool operator()(Node a,Node b){ if(a.x==b.x) return a.y> b.y; return a.x>b.x; } }; bool cmp(int a,int b){ return a>b; } char s[2100],t[2100]; int dp[2100][2100]; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int k; scanf("%s %s %d",s+1,t+1,&k); int n=strlen(s+1); int m=strlen(t+1); //dp[0][0]=0; for(int i=0;i<=n;i++)dp[i][0]=k*i; for(int i=0;i<=m;i++)dp[0][i]=k*i; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ dp[i][j]=min(dp[i-1][j-1]+abs(s[i]-t[j]),min(dp[i-1][j],dp[i][j-1])+k); } } cout<<dp[n][m]<<endl; return 0; }