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

最短公共子序列

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 case
T no of input string will be given to you.
E.g.
3
abcd abxy
sghk rfgh
svd vjhfd
Constrain 
1≤ length (string1) ≤100
1≤ length (string2) ≤100
Output:
Print the length of the shortest super sequence formed from these two strings.

Example

    T=3
Input:
abcd abxy
Output:
6 (abcdxy)
Input:
sghk rfgh
Output:
6 (srfghk)
Input:
svd vjhfd
Output:
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:

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

  1. 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”。

  2. 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”。

  3. 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:

考慮到兩個事實:

  1. 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
    
    
  2. 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"

Shortest Common Super Sequence

Problem solution:

問題方案:

Recursive algorithm:

遞歸算法:

    int Function(str1,pos1,str2,pos2):
if pos1>=str1.length() || pos2>=str2.length()
return 0
for a=pos1 to 0 
for b=pos1 to 0
if str[a]==str[a+l-1]
return Function(str1,a-1,str2,b-1)+1
else
return 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]+1
else
arr[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

最短公共子序列

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377569.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377569.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377569.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

單調棧 leetcode整理(二)

目錄為什么單調棧的時間復雜度是O(n)496. 下一個更大元素 I方法一&#xff1a;暴力方法二:單調棧哈希表739. 每日溫度單調棧模版解優化503. 下一個更大元素 II單調棧循環遍歷為什么單調棧的時間復雜度是O(n) 盡管for 循環里面還有while 循環&#xff0c;但是里面的while最多執…

Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解決辦法)

ZZ&#xff1a;http://www.blogjava.net/anchor110/articles/355699.html1、在工程下新建lib文件夾&#xff0c;將需要的第三方包拷貝進來。2、將引用的第三方包&#xff0c;添加進工作的build path。3、&#xff08;關鍵的一步&#xff09;將lib設為源文件夾。如果不設置&…

QTP自傳之web常用對象

隨著科技的進步&#xff0c;“下載-安裝-運行”這經典的三步曲已離我們遠去。web應用的高速發展&#xff0c;改變了我們的思維和生活習慣&#xff0c;同時也使web方面的自動化測試越來越重要。今天&#xff0c;介紹一下我對web對象的識別&#xff0c;為以后的對象庫編程打下基礎…

leetcode中使用c++需要注意的點以及各類容器的初始化、常用成員函數

目錄1、傳引用2、vector使用初始化方法常用成員函數3、字符串string初始化方法常用成員函數4、哈希表 unordered_map初始化常用成員函數示例&#xff1a;計數器5、哈希集合 unordered_set初始化常用成員函數6、隊列 queue初始化成員函數7、棧stack初始化常用成員函數7、emplace…

Linq list 排序,Dictionary 排序

C# 對List成員排序的簡單方法 http://blog.csdn.net/wanzhuan2010/article/details/6205884 LINQ之路系列博客導航 http://www.cnblogs.com/lifepoem/archive/2011/12/16/2288017.html 用一句Linq把一個集合的屬性值根據條件改了&#xff0c;其他值不變 list去重 list.Where((x…

javascript Ajax 同步請求與異步請求的問題

先來看以下代碼&#xff1a; var flagtrue; var index0; $.ajax({url: "http://www.baidu.com/",success: function(data){flagfalse;} }); while(flag){index; } alert(index); 請問最后alert的index的結果是多少&#xff1f; 可能有人會說0唄。實際上卻沒那么簡單…

定義類的Python示例

The task to define a class in Python. 在Python中定義類的任務。 Here, we are defining a class named Number with an attribute num, initializing it with a value 123, then creating two objects N1 and N2 and finally, printing the objects memory locations and a…

十一、線性層

一、Linear Layers torch.nn.Linear(in_features, out_features, biasTrue, deviceNone, dtypeNone) 以VGG神經網絡為例&#xff0c;Linear Layers可以將特征圖的大小進行變換由(1,1,4096)轉換為(1,1,1000) 二、torch.nn.Linear實戰 將CIFAR-10數據集中的測試集二維圖像[6…

easyui plugin——etreegrid:CRUD Treegrid

昨天寫了一個koeasyui的同樣的實現&#xff0c;感覺寫的太亂&#xff0c;用起來十分麻煩&#xff0c;于是今天照著edatagrid&#xff0c;寫了一個etreegrid&#xff0c;這樣再用ko綁定就方便多了。 使用很簡單,$(tableId).etreegrid({idField:parentIdField:,treeField:,saveUr…

expr

expr在linux中 是一個功能非常強大的命令。通過學習做一個小小的總結。 1、計算字符串的長度。我們可以用awk中的length(s)進行計算。我們 也可以用echo中的echo ${#string}進行計算&#xff0c;當然也可以expr中的expr length $string 求出字符串的長度。舉 例[rootlocalhost …

leetcode 42. 接雨水 思考分析(暴力、動態規劃、雙指針、單調棧)

目錄題目思路暴力法動態規劃雙指針法單調棧題目 給定 n 個非負整數表示每個寬度為 1 的柱子的高度圖&#xff0c;計算按此排列的柱子&#xff0c;下雨之后能接多少雨水。 輸入&#xff1a;height [0,1,0,2,1,0,1,3,2,1,2,1] 輸出&#xff1a;6 解釋&#xff1a;上面是由數組…

chdir函數_PHP chdir()函數與示例

chdir函數PHP chdir()函數 (PHP chdir() function) The full form of chdir is "Change Directory", the function chdir() is used to change the current working directory. chdir的完整形式是“更改目錄” &#xff0c; 功能chdir()用于更改當前工作目錄。 Synt…

十二、Sequential

一、Sequential介紹 torch.nn.Sequential(*args) 由官網給的Example可以大概了解到Sequential是將多層網絡進行便捷整合&#xff0c;方便可視化以及簡化網絡復雜性 二、復現網絡模型訓練CIFAR-10數據集 這里面有個Hidden units隱藏單元其實就是連個線性層 把隱藏層全部展開整…

1064-快速排序

描述 給定輸入排序元素數目n和相應的n個元素&#xff0c;寫出程序&#xff0c;利用內排序算法中快速排序算法進行排序&#xff0c;并輸出排序最后結果的相應序列。 輸入 共兩行&#xff0c;第一行給出排序元素數目n&#xff0c;第二行給出n個元素&#xff0c;1≤n≤100000&…

社交問答:取代BBS的Web2.0革命

編者按&#xff1a;本文由樂維UP創始人俞越撰寫&#xff0c;你也可以點擊這里關注俞越的新浪微博。 BBS在中國的興起是在95年&#xff0c;之后以驚人的速度發展起來。從2011年開始&#xff0c;國內的問答社區也如當年的BBS一樣&#xff0c;大量涌現快速成長&#xff0c;大體分為…

單調棧 leetcode整理(三)

目錄42. 接雨水思路分析901. 股票價格跨度思路581. 最短無序連續子數組思路一&#xff1a;排序雙指針思路二&#xff1a;單調棧思路三&#xff1a;雙指針(最省時)42. 接雨水 42. 接雨水 給定 n 個非負整數表示每個寬度為 1 的柱子的高度圖&#xff0c;計算按此排列的柱子&…

python 摳圖 鋸齒_Python | 繪圖中的抗鋸齒

python 摳圖 鋸齒Antialiasing is another important feature of Matplotlib and in this article, we will review how to use this functionality. pyplot.antialiased() is an inbuilt function in matplotlib.pyplot which performs our required operation. 抗鋸齒是Matpl…

apk 反編譯

引用&#xff1a;http://code.google.com/p/dex2jar/issues/detail?id20 最新版:dex2jar http://code.google.com/p/dex2jar/downloads/list 錯誤&#xff1a;http://code.google.com/p/dex2jar/issues/detail?id20 這段時間在學Android應用開發&#xff0c;在想既然是用Jav…

OpenDiscussion_DataDrivenDesign

本文源于公司內部技術交流&#xff0c;如有不當之處&#xff0c;還請指正。 Content&#xff1a; 1. What is Data-driven design?2. WPF revolution.3. More about ObservableCollection.4. Question.1. What is Data-driven design?Data-driven design: is a design of usi…

十三、Loss Functions

一、Loss Functions損失函數 損失函數的作用&#xff1a; 1&#xff0c;損失函數就是實際輸出值和目標值之間的差 2&#xff0c;由這個差便可以通過反向傳播對之后的數據進行更新 Loss Functions官網給的API 里面由很多種損失函數&#xff0c;不同的損失函數有其不同的用途及表…