查找两个字符串中相同字符串_使两个字符串相同的最低成本
查找兩個(gè)字符串中相同字符串
Problem statement:
問(wèn)題陳述:
Given two strings string1 and string2 find the minimum cost required to make the given two strings identical. We can delete characters from both the strings. The cost of deleting a character from string1 is costX and string2 is costY. The cost of removing any characters from the same string is the same. Like, removing any
給定兩個(gè)字符串, string1和string2找到使給定兩個(gè)字符串相同所需的最低成本。 我們可以從兩個(gè)字符串中刪除字符。 從string1刪除字符的成本為costX,而string2為costY 。 從同一字符串中刪除任何字符的成本是相同的。 喜歡,刪除任何
Input:The first line contains integers costX and costY.The second line contains the two strings X and Y.Output:Print the total cost to make the two strings equal for each test case in a new line.Constraints:1<= length of strings X and Y <=10001<= costX, costY <=1000Explanation of Example:
示例說(shuō)明:
Input:110 20"acba" "acdb"Output:30Explanation:The similar strings would be "acb". So need to remove one from string1 and one from string2 costing total of 30.Solution Approach:
解決方法:
The problem can be solved recursively,
該問(wèn)題可以遞歸解決,
Let,
讓,
N = length of string1M = length of string1F(n,m) = minimum cost to make two strings similarLet us consider, what can be cases that can arrive for F(i,j) where 0 <= i<n && 0 <= j<m
讓我們考慮一下,對(duì)于F(i,j) ,其中0 <= i <n && 0 <= j <m
Case 1.
情況1。
string1[i]==string2[j] that is indexed characters are similar
索引字符的string1 [i] == string2 [j]相似
In such a case, we don't need any additional cost to make strings similar, the cost will be similar to sub-problem of size i-1, j-1. This can be recursively written as
在這種情況下,我們不需要使字符串相似的任何額外費(fèi)用,該費(fèi)用將類(lèi)似于大小為i-1 , j-1的子問(wèn)題。 這可以遞歸寫(xiě)成
F(i,j) = F(i-1,j-1) if string1[i] == string2[j]Case 2.
情況2
string1[i]!=string2[j] that is indexed characters are not similar.
索引字符的string1 [i]!= string2 [j]不相似。
In such a case we need to invest,
在這種情況下,我們需要投資,
One thing can remove the indexed character from string1 and change to indexed string2 character.
一件事可以從字符串 1中刪除索引的字符,然后更改為索引的string2字符。
This can be recursively written as,
可以將其寫(xiě)為
F(i,j) = F(i-1,j) + costX if string1[i] != string2[j]Another way can be to remove the indexed character from string2 and change to string1 indexed character.
另一種方法是從string2刪除索引字符,然后更改為string1索引字符。
This can be recursively written as,
可以將其寫(xiě)為
F(i,j) = F(i,j-1) + costY if string1[i] != string2[j]Remove characters from both.
從兩者中刪除字符。
This can be recursively written as,
可以將其寫(xiě)為
F(i,j) = F(i-1,j-1) + costY + costX if string1[i] != string2[j] Finally, we would take minimum out of this three cases.So here goes the problem structure,
所以這里是問(wèn)題的結(jié)構(gòu),
Now the above recursion will create many overlapping subproblems and hence we need two converts it into DP.
現(xiàn)在,上述遞歸將創(chuàng)建許多重疊的子問(wèn)題,因此我們需要兩次將其轉(zhuǎn)換為DP。
Converting into DP
轉(zhuǎn)換為DP
n = string1 lengthm = string2 lengthstr1 = string1str2 = string21) Declare int dp[n+1][m+1];2) Base casefor i=0 to ndp[i][0]=i*costX;for j=0 to mdp[0][j]=j*costY;3) Fill the DPfor i=1 to nfor j=1 to m//first case when str1[i-1]==str2[j-1]dp[i][j]=dp[i-1][j-1]; if(str1[i-1]!=str2[j-1])dp[i][j]+=costX+costY;dp[i][j]=min(dp[i][j],min(dp[i-1][j]+costX,dp[i][j-1]+costY));end ifend forend for4) return dp[n][m];C++ Implementation:
C ++實(shí)現(xiàn):
#include <bits/stdc++.h> using namespace std;int editdistance(string s1, string s2, int costX, int costY) {int n = s1.length();int m = s2.length();int dp[n + 1][m + 1];for (int i = 0; i <= n; i++)dp[i][0] = i * costX;for (int j = 0; j <= m; j++)dp[0][j] = j * costY;for (int i = 1; i <= n; i++) {for (int j = 1; j <= m; j++) {dp[i][j] = dp[i - 1][j - 1];if (s1[i - 1] != s2[j - 1]) {dp[i][j] += costX + costY;dp[i][j] = min(dp[i][j], min(dp[i - 1][j] + costX, dp[i][j - 1] + costY));}}}return dp[n][m]; }int main() {int costX, costY;cout << "Cost of changing/removing character from string1: \n";cin >> costX;cout << "Cost of changing/removing character from string2: \n";cin >> costY;string s1, s2;cout << "Input string1\n";cin >> s1;cout << "Input string2\n";cin >> s2;cout << "Minimum cost to make two string similar: " << editdistance(s1, s2, costX, costY) << endl;return 0; }Output
輸出量
Cost of changing/removing character from string1: 2 Cost of changing/removing character from string2: 3 Input string1 includehelp Input string2 includuggu Minimum cost to make two string similar: 22翻譯自: https://www.includehelp.com/icp/minimum-cost-to-make-two-strings-identical.aspx
查找兩個(gè)字符串中相同字符串
總結(jié)
以上是生活随笔為你收集整理的查找两个字符串中相同字符串_使两个字符串相同的最低成本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python点线图_Python | 点
- 下一篇: java nextlong_Java R