サブシーケンスとして2つ以上のシーケンスを含む最短のスーパーシーケンスを見つけるためのC++プログラム
ここでは、サブシーケンスとして2つ以上のシーケンスを含む最短のスーパーシーケンスを見つけるためのC++プログラムについて説明します。
アルゴリズム
Begin function ShortestSubSeq() returns supersequence of A and B: 1) Declare an array ss[i][j] which contains length of shortest supersequence for A[0 .. i-1] and B[0 .. j-1]. 2) Find the length of the possible supersequence in bottom up manner using recursion. 3) Declare an array ss[i][j] which stores length of shortest supersequence for A[0 .. i-1] and B[0 .. j-1] in index. 4) Declare a string s to store the shortest subsequence. 5) Initialize i = a, j = b. 6) while (i > 0 and j > 0) A) If current character in A and B are same, then current character is part of shortest supersequence. Put current character in result. Reduce values of i, j and index. B) Else if If current character in A and B are different, Put current character of B in result. Reduce values of j and index. C) Else Put current character of A in result. Reduce values of i and index. 7) While (i > 0) Put remaining characters of A in the result string. 8) While(j>0) Put remaining characters of B in the result string. 9) Reverse the string and return it. End
例
#include <bits/stdc++.h>
using namespace std;
string ShortestSuperSeq(string A, string B) {
int a = A.length();
int b = B.length();
int ss[a + 1][b + 1];
for (int i = 0; i <= a; i++) {
for (int j = 0; j <= b; j++) {
if(i == 0)
ss[i][j] = j;
else if(j == 0)
ss[i][j] = i;
else if(A[i - 1] == B[j - 1])
ss[i][j] = 1 + ss[i - 1][j - 1];
else
ss[i][j] = 1 + min(ss[i - 1][j], ss[i][j - 1]);
}
}
int index = ss[a][b];
string s;
int i = a, j = b;
while (i > 0 && j > 0) {
//If current character in A and B are same, then current character is part of shortest supersequence
if (A[i - 1] == B[j - 1]) {
//Put current character in result.
//reduce values of i, j and index.
s.push_back(A[i - 1]);
i--, j--, index--;
}
//If current character in A and B are different,
else if (ss[i - 1][j] > ss[i][j - 1]) {
//Put current character of B in result.
//reduce values of j and index.
s.push_back(B[j - 1]);
j--, index--;
}
//Put current character of A in result.
//reduce values of i and index.
else {
s.push_back(A[i - 1]);
i--, index--;
}
}
//put remaining characters of A in the result string.
while (i > 0) {
s.push_back(A[i - 1]);
i--, index--;
}
//put remaining characters of B in the result string.
while (j > 0) {
s.push_back(B[j - 1]);
j--, index--;
}
reverse(s.begin(), s.end()); //Reverse the string and return it.
return s;
}
int main() {
string M = "ABBCDDEEFF";
string N = "ABCDEEEFF";
cout <<"The Shortest SuperSequence is:"<< ShortestSuperSeq(M, N);
return 0;
} 出力
The Shortest SuperSequence is:ABBCDEDEEFF
-
C++で三角形の図心を見つけるプログラム
この問題では、三角形の3つの頂点の座標を示す2D配列が与えられます。私たちのタスクは、C++で三角形のセントロイドを見つけるプログラムを作成することです。 セントロイド 三角形の3つの中央値は、三角形の3つの中央値が交差する点です。 中央値 三角形の頂点は、三角形の頂点とその反対側の線の中心点を結ぶ線です。 問題を理解するために例を見てみましょう 入力 (-3, 1), (1.5, 0), (-3, -4) 出力 (-3.5, -1) 説明 Centroid (x, y) = ((-3+2.5-3)/3, (1 + 0 - 4)/3) = (-3.5, -1) ソリューションアプロ
-
C++で平行四辺形の面積を見つけるプログラム
この問題では、平行四辺形の底と高さを表す2つの値が与えられます。私たちのタスクは、C++で平行四辺形の領域を見つけるプログラムを作成することです。 平行四辺形 は、反対側が等しく平行な4辺の閉じた図形です。 問題を理解するために例を見てみましょう 入力 B = 20, H = 15 出力 300 説明 平行四辺形の面積=B* H =20 * 15 =300 ソリューションアプローチ この問題を解決するために、平行四辺形の面積の幾何学的公式を使用します。 Area = base * height. ソリューションの動作を説明するプログラム 例 #include <io