這是bfSM演算法的源代碼。程式應顯示與模式完全匹配的部分的起始索引,如果在給定文本中沒有匹配,則顯示 -1。我嘗試在編程時包含迄今為止我使用過的所有庫,但是當我除錯程式時,控制臺上除了“(行程 15936)以代碼 0 退出”之外什么都沒有顯示。我不確定我到底錯過了什么,希望能得到一些幫助。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
#include<stdio.h>
#include<string.h>
using namespace std;
int bruteForceSM(string p, string t) {
for (int i = 0; i < (t.size() - p.size()); i ) {
int j = 0;
while(j < p.size() && p[j] == t[i j]) { j ; }
if (j == p.size()) { return i; }
}
return -1;
}
int main(){
string text = "sally sells seashells by the seashore";
string pattern = "shell";
bruteForceSM(pattern, text);
return 0;
}
uj5u.com熱心網友回復:
您從不列印結果,這就是您看不到任何結果的原因。在主函式中,替換
bruteForceSM(pattern, text);
和
cout << "Index at which pattern is found: " << bruteForceSM(pattern, text) << endl;
這將列印
Index at which pattern is found: 15
作為附加的一般建議:永遠不要使用using namespace std;(有關原因的更多資訊,請參閱為什么“使用命名空間 std;”被認為是不好的做法?)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/433595.html
上一篇:使用mysqlmax函式時缺少行
下一篇:啟用計時器時顯示標題
