日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

查找两个字符串中相同字符串_使两个字符串相同的最低成本

發(fā)布時(shí)間:2025/3/11 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 查找两个字符串中相同字符串_使两个字符串相同的最低成本 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

查找兩個字符串中相同字符串

Problem statement:

問題陳述:

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

給定兩個字符串, string1string2找到使給定兩個字符串相同所需的最低成本。 我們可以從兩個字符串中刪除字符。 從string1刪除字符的成本為costX,string2costY 。 從同一字符串中刪除任何字符的成本是相同的。 喜歡,刪除任何

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 <=1000

Explanation of Example:

示例說明:

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,

該問題可以遞歸解決,

Let,

讓,

N = length of string1M = length of string1F(n,m) = minimum cost to make two strings similar

Let us consider, what can be cases that can arrive for F(i,j) where 0 <= i<n && 0 <= j<m

讓我們考慮一下,對于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)用將類似于大小為i-1 , j-1的子問題。 這可以遞歸寫成

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,

    可以將其寫為

    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,

    可以將其寫為

    F(i,j) = F(i,j-1) + costY if string1[i] != string2[j]
  • Remove characters from both.

    從兩者中刪除字符。

    This can be recursively written as,

    可以將其寫為

    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,

    所以這里是問題的結(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)建許多重疊的子問題,因此我們需要兩次將其轉(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

    查找兩個字符串中相同字符串

    總結(jié)

    以上是生活随笔為你收集整理的查找两个字符串中相同字符串_使两个字符串相同的最低成本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。