Rokua P1279 String Distance

Source: Internet
Author: User

Title Description

There is a string x, we call the X at the tail and the middle of the insertion of any number of spaces after the new string is an extension of x, such as the string x is "Abcbcd", the string "ABCB-CD", "-a-bcbcd-" and "abcb-cd-" are the extension string of X, here "-" Represents a space character.

If A1 is the extension string of the strings A, B1 is the extension string of the strings B, A1 and B1 have the same length, then I have to define the distance of the string A1 and B1 to the sum of the characters at the corresponding position, while the distance of two non-whitespace characters is defined as the absolute value of the difference of their ASCII code. The distance between the space character and any other character is a known value of K, and the distance between the space character and the space character is 0. In all extension strings of string A and B, there must be two equal length extension strings A1, B1, so that the distance between A1 and B1 is minimized, and we define this distance as the distance between the string A and B.

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

input/output format Input Format:

Input file first behavior string A, second behavior string B. Both A and B are made up of lowercase letters and are not more than 2000 in length. The third behavior is an integer K (1≤k≤100) that represents the distance between the space and other characters.

output format:

The output file contains only one line with an integer representing the distance from the evaluated string A, B.

input/Output sample Input Sample # #:
Cmcsnmn2
Sample # # of output:
10

The ordinary dp,f[i][j] represents the two strings each match to the i,j position of the optimal solution, and then each decision to add a space or match the next character.

1 /**/2#include <iostream>3#include <cstdio>4#include <cmath>5#include <cstring>6#include <algorithm>7 using namespacestd;8 Const intmxn=2400;9 intF[MXN][MXN];Ten CharS1[MXN],S2[MXN]; One intLen1,len2; A intdis; - intMain () { -Memset (F, +,sizeoff); thescanf"%s\n%s", S1,S2); -scanf"%d",&dis); -Len1=strlen (S1); len2=strlen (S2); -     inti,j; +f[0][0]=0; -      for(i=1; i<=len1;i++) f[i][0]=f[i-1][0]+dis; +      for(i=1; i<=len2;i++) f[0][i]=f[0][i-1]+dis; A      for(i=1; i<=len1;i++) at        for(j=1; j<=len2;j++){ -F[i][j]=min (f[i][j-1],f[i-1][j]); -F[i][j]=min (f[i][j]+dis,f[i-1][j-1]+abs (s1[i-1]-s2[j-1])); -       } -printf"%d\n", F[len1][len2]);  -     return 0; in}

Rokua P1279 String Distance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.