使用c++;解决文字搜索问题。 很抱歉,每个人都有一个简单的问题,但是我对C++非常陌生,我不知道该怎么办。因此,我在C++中得到了一个框架,它在2个TXT文件中读取一个单词搜索和一个单词列表,并将结果显示在另一个TXT文件中。 我正在尝试完成这个框架,但我无法确定程序将如何准确地检查所有8个方向,或者系统将如何将字典文件中的单词与wordsearch进行比较,并将任何匹配的单词放入results.txt文件中。我已经附上了下面的框架,任何关于如何开始的信息或建议都将不胜感激 dictionary.txt ABSEIL BIKE PIXEL RESIGHTS YARDWORKS wordsearch.txt S T H G I S E R B G K L L B X D I E K P R H I M K L D T G Y O L E X I P B A P T W H T E J T Q U D X D W S F V M V S H L R B A H Q L B C K S A Y R D G B F J P Q Y Wordsearch.h #pragma once #include <iostream> #include <fstream> #include <string> #include <vector> #include <Windows.h> using namespace std; class WordSearch { public: WordSearch(); ~WordSearch(); bool ReadPuzzle(); bool ReadDictionary(); bool SolvePuzzleWithDictionary(); void WriteResults(string fileName); private: LARGE_INTEGER start, end, frequency; const int NUMBER_OF_RUNS; const string PUZZLE_NAME; const string DICTIONARY_NAME; }; Wordsearch.cpp #include "WordSearch.h" #include <algorithm> #include <iterator> #include <iostream> #include <iomanip> using namespace std; WordSearch::WordSearch() : NUMBER_OF_RUNS(500), PUZZLE_NAME("wordsearch_grid.txt"), DICTIONARY_NAME("dictionary.txt") { } WordSearch::~WordSearch() { } bool WordSearch::ReadPuzzle() { cout << endl << "ReadPuzzle() has NOT been implemented" << endl; return true; } bool WordSearch::ReadDictionary() { cout << endl << "ReadDictionary() has NOT been implemented" << endl; return true; } bool WordSearch::SolvePuzzleWithDictionary() { cout << endl << "SolvePuzzleWithDictionary() has NOT been implemented" << endl; double timeTakenInSeconds; QueryPerformanceFrequency(&frequency); QueryPerformanceCounter(&start); for (int n = 0; n < NUMBER_OF_RUNS; ++n) { // Add solving code here! } QueryPerformanceCounter(&end); timeTakenInSeconds = (end.QuadPart - start.QuadPart) / (double)(frequency.QuadPart*NUMBER_OF_RUNS); cout << fixed << setprecision(10) << "SolvePuzzleWithDictionary() - " << timeTakenInSeconds << " seconds" << endl; return false; } void WordSearch::WriteResults(string fileName) { cout << "WriteResults() has NOT been implemented" << endl; } dictionary.txt 下降 自行车 像素 辞职 院子 wordsearch.txt S T H G I S E R B G K L B X D I E K P R H I M K L D T G Y O L E X I P B A P T W H T E J T Q U D X D W S F V M V S H L R B A H Q L B C K S A Y R D G B F J P Q Y Wordsearch.h #布拉格语一次 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 类词搜索 { 公众: 单词搜索(); ~WordSearch(); bool ReadPuzzle(); bool ReadDictionary(); bool solveWithDictionary(); 无效写入结果(字符串文件名); 私人: 大整数开始、结束、频率; const int运行次数; 常量字符串名称; 常量字符串字典名称; }; Wordsearch.cpp #包括“WordSearch.h” #包括 #包括 #包括 #包括 使用名称空间std; WordSearch::WordSearch():运行次数(500次)、拼图名称(“WordSearch\u grid.txt”)、字典名称(“DICTIONARY.txt”) { } WordSearch::~WordSearch() { } bool WordSearch::ReadPuzzle() { cout

使用c++;解决文字搜索问题。 很抱歉,每个人都有一个简单的问题,但是我对C++非常陌生,我不知道该怎么办。因此,我在C++中得到了一个框架,它在2个TXT文件中读取一个单词搜索和一个单词列表,并将结果显示在另一个TXT文件中。 我正在尝试完成这个框架,但我无法确定程序将如何准确地检查所有8个方向,或者系统将如何将字典文件中的单词与wordsearch进行比较,并将任何匹配的单词放入results.txt文件中。我已经附上了下面的框架,任何关于如何开始的信息或建议都将不胜感激 dictionary.txt ABSEIL BIKE PIXEL RESIGHTS YARDWORKS wordsearch.txt S T H G I S E R B G K L L B X D I E K P R H I M K L D T G Y O L E X I P B A P T W H T E J T Q U D X D W S F V M V S H L R B A H Q L B C K S A Y R D G B F J P Q Y Wordsearch.h #pragma once #include <iostream> #include <fstream> #include <string> #include <vector> #include <Windows.h> using namespace std; class WordSearch { public: WordSearch(); ~WordSearch(); bool ReadPuzzle(); bool ReadDictionary(); bool SolvePuzzleWithDictionary(); void WriteResults(string fileName); private: LARGE_INTEGER start, end, frequency; const int NUMBER_OF_RUNS; const string PUZZLE_NAME; const string DICTIONARY_NAME; }; Wordsearch.cpp #include "WordSearch.h" #include <algorithm> #include <iterator> #include <iostream> #include <iomanip> using namespace std; WordSearch::WordSearch() : NUMBER_OF_RUNS(500), PUZZLE_NAME("wordsearch_grid.txt"), DICTIONARY_NAME("dictionary.txt") { } WordSearch::~WordSearch() { } bool WordSearch::ReadPuzzle() { cout << endl << "ReadPuzzle() has NOT been implemented" << endl; return true; } bool WordSearch::ReadDictionary() { cout << endl << "ReadDictionary() has NOT been implemented" << endl; return true; } bool WordSearch::SolvePuzzleWithDictionary() { cout << endl << "SolvePuzzleWithDictionary() has NOT been implemented" << endl; double timeTakenInSeconds; QueryPerformanceFrequency(&frequency); QueryPerformanceCounter(&start); for (int n = 0; n < NUMBER_OF_RUNS; ++n) { // Add solving code here! } QueryPerformanceCounter(&end); timeTakenInSeconds = (end.QuadPart - start.QuadPart) / (double)(frequency.QuadPart*NUMBER_OF_RUNS); cout << fixed << setprecision(10) << "SolvePuzzleWithDictionary() - " << timeTakenInSeconds << " seconds" << endl; return false; } void WordSearch::WriteResults(string fileName) { cout << "WriteResults() has NOT been implemented" << endl; } dictionary.txt 下降 自行车 像素 辞职 院子 wordsearch.txt S T H G I S E R B G K L B X D I E K P R H I M K L D T G Y O L E X I P B A P T W H T E J T Q U D X D W S F V M V S H L R B A H Q L B C K S A Y R D G B F J P Q Y Wordsearch.h #布拉格语一次 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 类词搜索 { 公众: 单词搜索(); ~WordSearch(); bool ReadPuzzle(); bool ReadDictionary(); bool solveWithDictionary(); 无效写入结果(字符串文件名); 私人: 大整数开始、结束、频率; const int运行次数; 常量字符串名称; 常量字符串字典名称; }; Wordsearch.cpp #包括“WordSearch.h” #包括 #包括 #包括 #包括 使用名称空间std; WordSearch::WordSearch():运行次数(500次)、拼图名称(“WordSearch\u grid.txt”)、字典名称(“DICTIONARY.txt”) { } WordSearch::~WordSearch() { } bool WordSearch::ReadPuzzle() { cout,c++,wordsearch,C++,Wordsearch,您可能希望有一个索引增量数组,用于在所有方向上移动(前提是将字段存储在矩阵中),以便编写尝试所有方向的通用代码: const int dirIncr[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}/*...*/} 因此,您可以循环遍历所有可能的起始单元格,并从每个可能的方向进行销售搜索(在每个步骤中,您必须检查当前坐标是否仍然适合您的数组) 至于实际搜索,这里可能会有所帮助。作为提示,如果您有一个与行对应的字符串数组,那么正常的水平搜索是微不足道的。因此,您也

您可能希望有一个索引增量数组,用于在所有方向上移动(前提是将字段存储在矩阵中),以便编写尝试所有方向的通用代码:

const int dirIncr[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}/*...*/}
因此,您可以循环遍历所有可能的起始单元格,并从每个可能的方向进行销售搜索(在每个步骤中,您必须检查当前坐标是否仍然适合您的数组)


至于实际搜索,这里可能会有所帮助。

作为提示,如果您有一个与行对应的字符串数组,那么正常的水平搜索是微不足道的。因此,您也可以将类似的逻辑应用到其他方向:为反向水平、垂直等构造字符串数组。这将加快单词搜索速度,但会牺牲set增加成本。我想我会使用矩阵方法(特别是当你需要搜索对角线模式时)。找到起始字母,然后查看所有相邻字母的匹配,移动到(x,y)如果不确定这是否是最快的,那么最好先从水平和垂直两个方向开始寻找一个完整的匹配(就像芭丝谢芭建议的那样),然后(如果没有找到)转到更复杂的搜索。