C++ 无效字谜解算器
以下程序基本上会生成一个字谜并继续求解:C++ 无效字谜解算器,c++,algorithm,anagram,C++,Algorithm,Anagram,以下程序基本上会生成一个字谜并继续求解: #include<iostream> #include<fstream> #include<algorithm> #include<vector> #include<string> #include<time.h> using namespace std; int random(){ int val = rand() % 26+1; return val; } voi
#include<iostream>
#include<fstream>
#include<algorithm>
#include<vector>
#include<string>
#include<time.h>
using namespace std;
int random(){
int val = rand() % 26+1;
return val;
}
void main(){
srand(time(NULL));
int b = 0, j = 0, x = 0; int count = 0;
string line;
vector<string>str;
vector<string>chk;
vector < int > v(8);
vector <char> v1;
vector <char > ::iterator p;
vector <int > ::iterator p3;
vector <char > ::iterator p2;
while (b != 1){
for (int i = 0; i < 8; i++){
v[i] = random();
}
for (int i = 0; i < 8; i++){
v1.push_back('a' + (v[i]-1));
cout << v1[i];
}
string nam(v1.begin(), v1.end());
sort(nam.begin(), nam.end());
do {
str.push_back(nam);
count++;
} while (next_permutation(nam.begin(),nam.end()));
std::sort(v.begin(), v.end());
char d = 'a'+v[7];//Comparision is case sensitive, so the letter should be lowercase//
cout << endl;
ifstream myfile("List of all words.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
if (line.find(d) == 0){
/*cout << line << "\n"; */
break;
}
else{
for (int i = 0; i < count; i++){
if (str[i].find(line) == 0){
cout << line << "\n";
break;
}
}
}
}
myfile.close();
}
cout << endl << count;
cin >> b;
}
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int random(){
int val=rand()%26+1;
返回val;
}
void main(){
srand(时间(空));
int b=0,j=0,x=0;int count=0;
弦线;
向量体;
向量机;
向量v(8);
向量v1;
向量::迭代器p;
向量::迭代器p3;
向量::迭代器p2;
而(b!=1){
对于(int i=0;i<8;i++){
v[i]=random();
}
对于(int i=0;i<8;i++){
v1.推回('a'+(v[i]-1));
cout你可以通过创建一个字谜字典来避免检查每一个排列与词汇表。这将需要更多的空间,但会加快搜索解决方案的速度
另一种更简单的方法是使用每个单词按字母顺序排列的字母组成的键来存储词典,这样就可以将候选词的列表减少到最小
这里有一个循序渐进的方法
第一步:使用适应的数据结构
我建议您在中加载文件,而不是每次按顺序浏览文件
// load accepted words
set<string>mydict;
ifstream myfile("List of all words.txt");
if(!myfile) {
cerr << "Dictionnary file couldn't be loaded !\n";
return 1;
}
istream_iterator<string> eof;
istream_iterator<string> iit(myfile);
copy(iit, eof, inserter(mydict, mydict.begin())); // copy file to set
cout << mydict.size() << " words loaded\n";
//加载接受的单词
setmydict;
ifstream myfile(“所有单词列表.txt”);
如果(!myfile){
cerr欢迎来到stackoverflow!请花一分钟时间阅读和。闲聊、开放式的问题会降低网站的实用性。如果这段代码有效,请将其交给CR.SEThanks进行回复。所以你基本上是建议我使用地图?我对此感到困惑,因为我不知道如何将两个不同的单词与e同一个键。@BilalMusani底线是您当前的代码进行线性搜索——如果考虑速度,这实际上是搜索项目最糟糕的方式。可能是映射、哈希映射、搜索树、二进制搜索(如果数据已经排序),除了缓慢的线性搜索方法外,什么都没有。@PaulMcKenzie是的!当我第一次在智能手机的小屏幕上阅读问题时,我没有注意到。所以我已经编辑了解决第一点(步骤1+2)@BilalMusani是一组字符串的映射(或多重映射)。同时,我编辑了这个问题,以便向您展示如何改进您的初始方法,然后如何使用这样的地图。如果您能将您注意到的性能改进作为评论发布,那将非常好。@Christophe非常感谢。性能确实有了显著的提高,不过,我将尝试使用自己的解决方案。@Christophe正在从代码中获取一些帮助。
while(b != 1)
{
count = 0;
v.clear();
for(int i = 0; i < 8; i++) // construct random anagram
v.push_back('a' + (random() - 1));
string nam(v.begin(), v.end());
cout << "Anagram:" << nam << endl;
std::sort(nam.begin(), nam.end()); // for using next permut()
do { // check if any of the permutation is in the set
if(mydict.find(nam) != mydict.end()) {
cout << "Found:" << nam << endl; // bingo !!!
count++;
}
} while(next_permutation(nam.begin(), nam.end()));
if(count == 0)
cout << "Not found!"<<endl;
cin >> b;
}
// compute anagram map
map<string, set<string>>myanadict;
for(auto x : mydict) { // for every word in the dictionaly
string alph=x;
sort(alph.begin(), alph.end()); // sort the letters
myanadict[alph].insert(x);
}
while(b != 1)
{
count = 0;
v.clear();
for(int i = 0; i < 8; i++)
v.push_back('a' + (random() - 1));
string nam(v.begin(), v.end());
//string nam = "dixeif"; // that was to test fixed values in my very smal file ;-)
cout << "Anagram:" << nam << endl;
std::sort(nam.begin(), nam.end()); // sort now for the map search
if (myanadict.find(nam)!=myanadict.end()) { // check if there's an entry
cout << "Found:"; // bingo !!!
copy(myanadict[nam].begin(), myanadict[nam].end(), ostream_iterator<string>(cout, " ")); // show all
}
else cout << "Not found!"<<endl;