gpt4 book ai didi

c++ - C++ Anagram Solver速度优化

转载 作者:行者123 更新时间:2023-12-01 14:53:05 26 4
gpt4 key购买 nike

我决定为我的父亲制作一个字谜解算器。我对编程很陌生,但我认为我仍然可以做到。我的成品可以用,但是速度确实很慢,例如找到8个字符的所有组合花了大约15分钟以上。我正在寻找对其进行优化/使其更快的方法。

在Clion 2019.3.4上使用MinGW c++编译器,CPU:i7 9700k,RAM:16GB / 3200mhz。

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

//Menu for interacting with user, not really important
void menu() {
cout << "=========================" << endl;
cout << "======== !WOW! ==========" << endl;
cout << "=========================" << endl;
cout << "1 ... INSERT" << endl;
cout << "2 ... PRINT" << endl;
cout << "3 ... LIMIT WORD LENTGH" << endl;
cout << "4 ... NEW GAME" << endl;
cout << "0 ... EXIT" << endl;
cout << "=========================" << endl;
cout << "Select: ";
}

//Function to find all possible combinations from letters of a given string
void get(vector<string> &vec, string str, string res) {

vec.push_back(res);

for (int i = 0; i < str.length(); i++)
get(vec, string(str).erase(i, 1), res + str[i]);
}

//Only for testing purposes
void printVec(vector<string> vec) {
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
}

//Function to check if a given word exists in given .txt file
bool checkWord(vector<string> &vec2, string filename, string search) {

string line;
ifstream myFile;
myFile.open(filename);

if (myFile.is_open()) {
while (!myFile.eof()) {
getline(myFile, line);
if (line == search) {
vec2.push_back(line);
return true;
}
}
myFile.close();
} else
cout << "Unable to open this file." << endl;

return false;
}


int main() {

int selection;
bool running = true;
string stringOfChars;
vector<string> vec;
vector<string> vec2;

do {

menu();
cin >> selection;
switch (selection) {

case 1:
cout << "Insert letters one after another: ";
cin >> stringOfChars;
get(vec, stringOfChars, ""); //fill first vector(vec) with all possible combinations.
break;

case 2:
for (int i = 0; i < vec.size(); i++) {
if (checkWord(vec2, "C:/file.txt", vec[i])) { //For each word in vector(vec) check if exists in file.txt, if it does, send it in vector(vec2) and return true
//Reason for vec2's existence is that later I want to implement functions to manipulate with possible solutions (like remove words i have already guessed, or as shown in case 3, to limit the word length)
cout << vec[i] << endl; //If return value == true cout the word
}
}
break;

case 3:
int numOfLetters;
cout << "Word has a known number of letters: ";
cin >> numOfLetters;
for (int i = 0; i < vec2.size(); i++) { /*vec2 is now filled with all the answers, we can limit the output if we know the length of the word */
if (vec2[i].length() == numOfLetters) {
cout << vec2[i] << endl;
}
}
break;

case 4:
vec.clear();
vec2.clear();
break;

case 0:
running = false;
break;

default:
cout << "Wrong selection!" << endl;
break;
}
cout << endl;
} while (running);

return 0;
}



file.txt充满我语言中的所有单词,按字母顺序排列,大小为50mb。
aachecnska
aachenskega
aachenskem
aachenski
.
.
.
bab
baba
babah
.
.
.

任何建议或主题外的提示都将有所帮助。我的想法之一是可能将file.txt分隔成较小的文件,例如将具有相同起始字母的行放在自己的文件中,因此A.txt仅包含以A开头的单词,等等。相应地编码。

最佳答案

这是您需要使用分析器的地方。在Linux上,我最喜欢的是kcachgrind

http://kcachegrind.sourceforge.net/html/Home.html

它为您提供逐行计时信息,并告诉您应该最优化代码的哪一部分。

当然,有很多可用的分析器,包括商用的。

关于c++ - C++ Anagram Solver速度优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61041984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com