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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

最短公共子序列_最短公共超序列

發布時間:2023/12/1 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最短公共子序列_最短公共超序列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最短公共子序列

Problem statement:

問題陳述:

Given two strings, you have to find the shortest common super sequence between them and print the length of the super sequence.

給定兩個字符串,您必須找到它們之間最短的公共超級序列,并打印超級序列的長度。

Input:T Test caseT no of input string will be given to you.E.g.3abcd abxysghk rfghsvd vjhfdConstrain 1≤ length (string1) ≤1001≤ length (string2) ≤100Output:Print the length of the shortest super sequence formed from these two strings.

Example

T=3Input:abcd abxyOutput:6 (abcdxy)Input:sghk rfghOutput:6 (srfghk)Input:svd vjhfdOutput:6 (svjhfd)

Explanation with example:

舉例說明:

Let there are two strings str1 and str2.

假設有兩個字符串str1和str2 。

str1 = "abcd" str2 = "abxy"

To find out the length of the super sequence from these two string is a bit difficult. To solve this problem we have to follow these steps:

從這兩個字符串中找出超級序列的長度有些困難。 要解決此問題,我們必須遵循以下步驟:

  • We have to find out the longest common subsequence between the two strings. From "abcd" and "abxy" the longest common subsequence is "ab".

    我們必須找出兩個字符串之間最長的公共子序列。 從“ abcd”和“ abxy”開始,最長的公共子序列是“ ab”。

  • After getting the longest common subsequence we will add the non-common elements with the longest common subsequence. Non-common characters from "abcd" is "cd" and from "abxy" is "xy".

    得到最長的公共子序列后,我們將添加具有最長的公共子序列的非公共元素。 來自“ abcd”的非常見字符為“ cd”,來自“ abxy”的非常見字符為“ xy”。

  • Therefore the length of the shortest common super sequence is:

    因此,最短公共超序列的長度為:

    length of the two strings-length of the longest common subsequencs The length of the shortest common super sequence is 6 (abcdxy) 6(abcdxy)
  • Using a dynamic programming algorithm to find out the longest common subsequence between two given string is very efficient and fast as compared to the recursion approach.

    與遞歸方法相比,使用動態編程算法找出兩個給定字符串之間最長的公共子序列非常有效且快速。

    Let f(a,b) = count the number of common subsequences from the two string starting from 0 to position a and starting from 0 to position b.

    令f(a,b) =計算兩個字符串中從0開始到位置a以及從0開始到位置b的公共子序列數。

    Considering the two facts:

    考慮到兩個事實:

  • If the character of string1 at index a and character of string1 at index b are the same then we have to count how many characters are the same between the two strings before these indexes? Therefore,

    如果索引a的字符串 1的字符和索引b的字符串 1的字符相同,那么我們必須計算在這些索引之前兩個字符串之間有多少個相同的字符? 因此,

    f(a,b)=f(a-1,b-1)+1
  • If the character of string1 at index a and character of string1 at index b are not same then we have to calculate the maximum common character count between (0 to a-1 of string1 and 0 to b of string2) and (0 to a of string1 and 0 to b-1 of string2).

    如果字符串 2中的索引和字符串1的字符索引b中的符不相同,則我們來計算之間的最大公共字符計數(0到字符串1的A-1和0至b 字符串2)和(0至a的string1和0到string2的 b-1 )。

    f(a,b)=max?(?(f(a-1,b),f(a,b-1))
  • For the two strings:

    對于兩個字符串:

    str1 = "abcd"str2 = "abxy"

    Problem solution:

    問題方案:

    Recursive algorithm:

    遞歸算法:

    int Function(str1,pos1,str2,pos2):if pos1>=str1.length() || pos2>=str2.length()return 0for a=pos1 to 0 for b=pos1 to 0if str[a]==str[a+l-1]return Function(str1,a-1,str2,b-1)+1elsereturn max(Function(str1,a,str2,b-1),Function(str1,a-1,str2,b));

    DP conversion:

    DP轉換:

    int Function(str1,str2):arr[len1+1][len2+1]for len=1 to str1.length()for a=1 to str2.length()if str[a]==str[b]arr[a][b]=arr[a-1][b-1]+1elsearr[a][b]=max(arr[a][b-1],arr[a-1][b])return arr[len1-1][len2-1]

    C++ Implementation:

    C ++實現:

    #include <bits/stdc++.h> using namespace std;int count_common_subsequence(string str1, string str2) {int len1 = str1.length();int len2 = str2.length();int arr[len1 + 1][len2 + 1];memset(arr, 0, sizeof(arr));for (int i = 1; i <= len1; i++) {for (int j = 1; j <= len2; j++) {if (str1[i - 1] == str2[j - 1]) {arr[i][j] = arr[i - 1][j - 1] + 1;}else {arr[i][j] = max(arr[i - 1][j], arr[i][j - 1]);}}}return arr[len1][len2]; }int main() {int t;cout << "Test Case : ";cin >> t;while (t--) {string str1, str2;cout << "Enter the strings : ";cin >> str1 >> str2;cout << "Length of the longest common subsequence: " << count_common_subsequence(str1, str2) << endl;}return 0; }

    Output

    輸出量

    Test Case : 3 Enter the strings : svd vjhfd Length of the longest common subsequence: 2 Enter the strings : sghk rfgh Length of the longest common subsequence: 2 Enter the strings : abcd abxy Length of the longest common subsequence: 2

    翻譯自: https://www.includehelp.com/icp/shortest-common-super-sequence.aspx

    最短公共子序列

    總結

    以上是生活随笔為你收集整理的最短公共子序列_最短公共超序列的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 不卡视频国产 | 美女91网站 | h在线播放 | 亚洲一区二区在线看 | 91啪在线观看 | 在线观看成年人视频 | caoprom在线视频| 国模视频在线 | 五月天激情综合网 | 一本到在线视频 | 欧美精品色图 | 日韩高清欧美 | 裸体一区二区 | 国产在线观看精品 | 久操综合| 91视频99| 欧洲一区在线观看 | 国产麻豆免费视频 | 国产91精品看黄网站在线观看 | 好色先生tv官网 | 美国黄色a级片 | 女人喷潮完整视频 | 欧美激情影院 | 亚洲高清无码久久 | 国产视频第一区 | 午夜av网| 国产精品九九视频 | 欧美日韩在线a | 七仙女欲春2一级裸体片 | 久久久久久久久久久影视 | 蜜桃av免费 | 精品成人网 | 日本中文字幕久久 | 亚洲男人网站 | 丰满岳乱妇一区二区三区 | 欧美美女爱爱视频 | 91av视频在线观看 | 色综合一区二区三区 | 国产三级日本三级在线播放 | 综合网色 | 制服诱惑一区 | 亚洲国产黄 | 亚欧成人精品一区二区 | 欧美一区二区三区久久久 | 日韩人妻精品无码一区二区三区 | 在线观看网站污 | 日本一二三区在线视频 | 在线观看免费www | 亚洲少妇自拍 | 日本三级久久久 | 欧洲一区在线观看 | 性囗交免费视频观看 | 天堂在线中文资源 | 五月婷婷六月丁香综合 | 先锋影音av资源在线 | 在线观看欧美日韩视频 | 98国产精品 | 黄色特级大片 | 国产精品视频在线免费观看 | 中文字幕乱轮 | 免费黄色小视频在线观看 | 999资源站 | 天堂av√ | 日韩有码中文字幕在线 | 日韩精品乱码久久久久久 | 日韩av在线网 | 欧美激情视频在线观看 | 久草免费福利视频 | 亚洲AV无码乱码国产精品牛牛 | 欧美日本色 | 日韩av在线免费看 | 久久久少妇| 亚洲色偷偷色噜噜狠狠99网 | 羞羞网站在线看 | wwwxxx日本人 | 欧美中文字幕一区二区 | av导航网址 | 亚洲一区二区三区不卡视频 | 轻点好疼好大好爽视频 | 国产日产精品一区 | 黄色在线免费观看视频 | 男女乱淫 | 97射射| а√天堂www在线天堂小说 | 亚洲免费精品视频 | 少妇av网 | 国产精品久久毛片 | 久久精品午夜福利 | 国产黄在线免费观看 | 成人午夜视频网站 | 黄色录像二级片 | 5a毛片 | 黄色一极视频 | 求av网址| 二级毛片视频 | 国产三级一区二区 | 成人精品在线观看 | 午夜嘿嘿嘿 | 黄色片aa |