同じテキストを繰り返し検索するC++プログラム(データ構造を構築することによる聖書など)
これは、同じテキストを繰り返し検索するC++プログラムです。
アルゴリズム
Begin Take the original string and pattern to be searched as input. org_len = store the length of original string pat_len = store the length of pattern for i = 0 to (org_len - pat_len) for j = 0 to pat_len - 1 if (org[i + j] != patt[j]) if (j == pat_len) Increase m. Print the position at which the pattern is found if (m == 0) Print no match found else Print the total number of instances found. return 0 End
例
#include<iostream>
#include<string.h>
using namespace std;
int main() {
char org[150], patt[150];
int i, j, m = 0, org_len, pat_len;
cout << "\nEnter Original String:";
cin >> org;
cout << "Enter Pattern to Search:";
cin >> patt;
org_len = strlen(org); //store the length of original string
pat_len = strlen(patt); //store the length of pattern
for (i = 0; i <= (org_len - pat_len); i++) {
for (j = 0; j < pat_len; j++) {
if (org[i + j] != patt[j])
break;
}
if (j == pat_len) {
m++;
cout << "\nPattern Found at Position: " << i;
}
} if (m == 0)
cout << "\nNo Match Found.";
else
cout << "\nTotal Number of Instances Found = " << m;
return 0;
} 出力
Enter Original String:thisistutorialspoint.thisisac++program Enter Pattern to Search:is Pattern Found at Position: 2 Pattern Found at Position: 4 Pattern Found at Position: 23 Pattern Found at Position: 25 Total Number of Instances Found = 4
-
C ++プログラムでの二分探索?
二分探索は、半区間探索、対数探索、または二分探索とも呼ばれ、ソートされた配列内のターゲット値の位置を見つける検索アルゴリズムです。二分探索は、ターゲット値を配列の中央の要素と比較します。それらが等しくない場合、ターゲットが存在できない半分が削除され、残りの半分で検索が続行され、再び中央の要素がターゲット値と比較され、ターゲット値が見つかるまでこれが繰り返されます。残りの半分が空の状態で検索が終了した場合、ターゲットは配列に含まれていません。アイデアは単純ですが、バイナリ検索を正しく実装するには、特に配列の値が範囲内の整数のすべてではない場合、終了条件と中間点の計算に関する微妙な点に注意する必要
-
C++でツリーを構築せずに同一のBSTを確認する
BSTの要素を表す2つの配列があります。その配列から要素を左から右に取得してBSTを形成する場合、両方の配列から取得することで、同じBSTを作成します。両方が同じように形成されているかどうかを確認する必要があります。しかし、制約は、BSTを作成できないことです。 2つの配列が{2、4、1、3}と{2、1、4、3}であるとすると、これらのシーケンスは両方とも同じBSTを形成できます。 アプローチは簡単です。ご存知のように、左側のサブツリーの要素はルートよりも小さく、右側のサブツリーの要素はルートよりも大きくなっています。これらの2つのリストは、各要素xについて、xの左右のサブツリーの要素