-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordle.cpp
More file actions
71 lines (64 loc) · 1.58 KB
/
Copy pathwordle.cpp
File metadata and controls
71 lines (64 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef RECCHECK
#include <iostream>
#include <map>
#include <set>
#include <string>
#endif
#include "wordle.h"
#include "dict-eng.h"
using namespace std;
static void erase_one_char(string& s, char c)
{
for(size_t i = 0; i < s.size(); ++i){
if(s[i] == c){
s.erase(i, 1);
return;
}
}
}
static void solve(const string& in,
const set<string>& dict,
string& cur,
int idx,
string floating_left,
set<string>& results)
{
int n = (int)in.size();
if(idx == n){
if(floating_left.empty() && dict.find(cur) != dict.end()){
results.insert(cur);
}
return;
}
int remaining = n - idx;
if((int)floating_left.size() > remaining){
return;
}
if(in[idx] != '-'){
cur[idx] = in[idx];
erase_one_char(floating_left, in[idx]);
solve(in, dict, cur, idx + 1, floating_left, results);
}
else{
for(char c = 'a'; c <= 'z'; ++c){
cur[idx] = c;
string nextFloating = floating_left;
erase_one_char(nextFloating, c);
int nextRemaining = remaining - 1;
if((int)nextFloating.size() > nextRemaining){
continue;
}
solve(in, dict, cur, idx + 1, nextFloating, results);
}
}
}
set<string> wordle(
const string& in,
const string& floating,
const set<string>& dict)
{
set<string> results;
string cur = in;
solve(in, dict, cur, 0, floating, results);
return results;
}