C++ 运行BruteForce算法时出错?
我接受用户的输入,如下所示:C++ 运行BruteForce算法时出错?,c++,string,algorithm,debugging,brute-force,C++,String,Algorithm,Debugging,Brute Force,我接受用户的输入,如下所示: algo_type "pattern" filename 前 到目前为止,我将用户输入分为三个不同的变量,一个用于algo_类型,一个用于我正在寻找的模式,一个用于文件名。一旦我得到了模式和文件名,我就试图将模式引入到Bruteforce算法中,搜索每一行并打印模式在.txt文件行中出现的位置。现在,尽管每次我将输入输入到algo时,它都返回-1,这意味着BruteForce没有运行?我到底做错了什么 int BruteForce(const string&am
algo_type "pattern" filename
前
到目前为止,我将用户输入分为三个不同的变量,一个用于algo_类型,一个用于我正在寻找的模式,一个用于文件名。一旦我得到了模式和文件名,我就试图将模式引入到Bruteforce算法中,搜索每一行并打印模式在.txt文件行中出现的位置。现在,尽管每次我将输入输入到algo时,它都返回-1,这意味着BruteForce没有运行?我到底做错了什么
int BruteForce(const string& line, const string& pattern){
int n , m;
n = line.length();
m = pattern.length();
for(int i = 0 ; i < n - m ; i++){
int j = 0;
while( j < m && line[i + j] == pattern[j]){
j = j+1;
if( j == m){
return i;
}
}
}
return -1;
}
int main(){
string text, algo_type , pattern , fname, line;
getline(cin ,text);
istringstream iss(text);
if(iss >> algo_type >> pattern >> fname){
cout << algo_type << pattern << fname << "'\n'";
}
int i = 0;
ifstream ifs;
ifs.open(fname.c_str());
while(getline(ifs, line) && fname != ""){
if( algo_type == "bf"){
cout << "Line " << i++ << ":" << BruteForce(line,pattern) << endl;
}
}
return 0;
}
int BruteForce(常量字符串和行、常量字符串和模式){
int n,m;
n=直线长度();
m=模式长度();
对于(int i=0;i>算法类型>>模式>>fname){
cout我想您希望在BruteForce的末尾返回-1,而不是在第一次迭代的末尾
另外,第一个循环条件需要有为什么你用我的答案中的修正来编辑你的问题?你是说即使编辑了,问题仍然存在吗?现在还不清楚我把return-1;放在了正确的位置。尽管我的蛮力算法不起作用。我想我如何读取.txt文件有问题吗?我您不使用std::string::find
或std::find
有什么原因吗?我添加了名称空间std;很抱歉没有说清楚。@sehe-这就是我想问的问题。谢谢您,我一定会添加该功能。@user2757849我想我已经找到了罪魁祸首。您应该注意常量
and signed/unsigned comparison:/Right ok,那么您的算法和我的算法在查找模式的第一个匹配项方面有相同的精确输出,现在的问题是其他匹配项发生在同一行中。因此,算法查找第一个匹配项,然后移动到下一行,而没有在lin中查找模式的其他匹配项e、 @user2757849就我而言,这很好。而且,您最初编写的算法没有找到相同的解决方案(因为这就是我修复第一个循环的条件的原因)你比我更完全对C++是全新的,并且可以做一些研究,我很感激帮助,尽管它对我理解和调试我的代码非常有帮助,非常感谢,对任何挫折感到抱歉。
int BruteForce(const string& line, const string& pattern){
int n , m;
n = line.length();
m = pattern.length();
for(int i = 0 ; i < n - m ; i++){
int j = 0;
while( j < m && line[i + j] == pattern[j]){
j = j+1;
if( j == m){
return i;
}
}
}
return -1;
}
int main(){
string text, algo_type , pattern , fname, line;
getline(cin ,text);
istringstream iss(text);
if(iss >> algo_type >> pattern >> fname){
cout << algo_type << pattern << fname << "'\n'";
}
int i = 0;
ifstream ifs;
ifs.open(fname.c_str());
while(getline(ifs, line) && fname != ""){
if( algo_type == "bf"){
cout << "Line " << i++ << ":" << BruteForce(line,pattern) << endl;
}
}
return 0;
}
#include <string>
using namespace std;
int BruteForce(const string& line, size_t start, const string& pattern) {
const size_t n = line.length();
const size_t m = pattern.length();
if (n<m) return -1;
for(size_t i = start; i <= (n - m); i++) {
for(size_t j=0; j < m && (line[i + j] == pattern[j]); ++j) {
if(j == m-1) {
return i;
}
}
}
return -1;
}
#include <iostream>
#include <fstream>
#include <sstream>
int main() {
string text, algo_type, pattern, fname, line;
getline(cin ,text);
istringstream iss(text);
if(iss >> algo_type >> pattern >> fname) {
cout << " " << algo_type << " " << pattern << " " <<fname << "\n";
}
int i = 1;
ifstream ifs;
ifs.open(fname.c_str());
while(getline(ifs, line) && fname != "") {
if(algo_type == "bf") {
int pos = -1;
while (-1 != (pos = BruteForce(line, pos+1, pattern)))
cout << "Line " << i << ":" << pos << " " << line << endl;
}
i++;
}
return 0;
}
./test <<< "bf iss /etc/dictionaries-common/words" | grep Miss