短いテキストサイズの文字列検索アルゴリズムを実装するC++プログラム
このC++プログラムでは、入力としてテキストとパターンが与えられます。出力として、パターンがテキスト内で検索され、パターンのすべてのインスタンスが指定されます。
アルゴリズム
Begin Take the string and pattern as input. Declare the original and duplicate array with their size. Put the lengths of original and duplicate in len_ori and len_dupli. Make a loop for find out the position of the searched pattern. If pattern is not found, print not found otherwise print the no of instances of the searched pattern. End
サンプルコード
#include<iostream>
#include<cstring>
using namespace std;
int main() {
char ori[120], dupli[120];
int i, j, k = 0, len_ori, len_dupli;
cout<<"enter string without any blank space"<<endl;
cout<<"\nEnter Original String:";
cin>>ori;
cout<<"Enter Pattern to Search:";
cin>>dupli;
len_ori = strlen(ori);
len_dupli = strlen(dupli);
for (i = 0; i <= (len_ori - len_dupli); i++) // loop to find out the position Of searched pattern {
for (j = 0; j < len_dupli; j++) {
if (ori[i + j] != dupli[j])
break ;
}
if (j == len_dupli) {
k++;
cout<<"\nPattern Found at Position: "<<i;
}
}
if (k == 0)
cout<<"\nNo Match Found!";
else
cout<<"\nTotal Instances Found = "<<k;
return 0;
} 出力
enter string without any blank space Enter Original String:Enter Pattern to Search: Pattern Found at Position: 0 Pattern Found at Position: 1 Pattern Found at Position: 2 Pattern Found at Position: 3 Total Instances Found = 4
-
Kadaneのアルゴリズムを実装するためのC++プログラム
Kadaneのアルゴリズムは、整数の配列から最大のサブ配列の合計を見つけるために使用されます。ここでは、このアルゴリズムを実装するためのC++プログラムについて説明します。 アルゴリズム Begin Function kadanes(int array[], int length): Initialize highestMax = 0 currentElementMax = 0 for i = 0 to length-1 currentElement
-
ヴィジュネル暗号を実装するためのC++プログラム
Vigenere Cipherは、アルファベットのテキストを暗号化する一種の多表式置換方法です。 この方法での暗号化と復号化には、AからZまでのアルファベットが26行で書き込まれるVigenereCipherTableが使用されます。 暗号化 キー: ようこそ メッセージ: Thisistutorialspoint ここでは、指定されたキーの長さが元のメッセージの長さと等しくなるまで、そのキーを繰り返してキーを取得する必要があります。 暗号化の場合は、メッセージの最初の文字とキー(TとW)を取得します。V行とW列が一致するVigenere Cipher Tableのアルファベ