-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem98.h
More file actions
169 lines (156 loc) · 5.09 KB
/
problem98.h
File metadata and controls
169 lines (156 loc) · 5.09 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// Created by Pratim, Sonia, Karun on 5/12/22.
//
#ifndef PROJECT_EULER_PROBLEM98_H
#define PROJECT_EULER_PROBLEM98_H
#include "unordered_map"
#include "unordered_set"
#include "iostream"
#include "string"
#include "vector"
#include "sstream"
#include "fstream"
#include "tuple"
#include "cmath"
#include "../include/string_lib.h"
using namespace std;
bool check_anagram(const string& a, const string& b) {
if (a.length() != b.length())
return false;
unordered_map<char, int> map;
for (int i = 0; i < a.size(); i++) {
map[a[i]] ++;
map[b[i]] --;
}
for (auto itr: map) {
if (itr.second != 0)
return false;
}
return true;
}
long get_pattern_word(const string& word) {
long pattern = 0;
unordered_map<char, int> map;
int index = 0;
int size = word.size();
for (int j = size - 1; j >= 0; j--) {
char c = word[j];
if (map.find(c) == map.end()) {
map[c] = index;
index += 1;
}
pattern += map[c] * pow(10, size-j-1);
}
return pattern;
}
long get_pattern_int(long n) {
long pattern = 0;
unordered_map<int, int> map;
int index = 0;
int i = 0;
while (n > 0) {
int d = n % 10;
if (map.find(d) == map.end()) {
map[d] = index;
index += 1;
}
pattern += map[d] * pow(10, i);
n = floor(n / 10);
i++;
}
return pattern;
}
vector<tuple<string, string>> get_anagram_pairs(const string& filename,
unordered_map<string, long>& pattern_map,
unordered_set<long>& pattern_set) {
ifstream file(filename);
string line;
getline(file, line);
istringstream iss(line);
vector<string> words = split(line, ',');
for (string& word: words) {
word = word.substr(1, word.length()-2);
}
vector<tuple<string, string>> anagram_pairs;
for (int i = 0; i < words.size(); i++) {
for (int j = i+1; j < words.size(); j++) {
if (check_anagram(words[i], words[j])) {
string word1 = words[i];
string word2 = words[j];
long pattern1 = get_pattern_word(word1);
long pattern2 = get_pattern_word(word2);
pattern_map[word1] = pattern1;
pattern_map[word2] = pattern2;
pattern_set.insert(pattern1);
pattern_set.insert(pattern2);
tuple<string, string> pair {word1, word2};
anagram_pairs.push_back(pair);
}
}
}
return anagram_pairs;
}
void create_square_map(unordered_map<long, unordered_set<long>>& square_map,
unordered_set<long>& pattern_set) {
long n = floor(10000 * sqrt(10)) - 1;
while (n > 0) {
long square = n * n;
int n_digits = ceil(log10(square));
long pattern = get_pattern_int(square);
if (pattern_set.find(pattern) != pattern_set.end()) {
if (square_map.find(pattern) == square_map.end())
square_map[pattern] = unordered_set<long> {};
square_map[pattern].insert(square);
}
n--;
}
}
long switch_digits(const string& word1, const string& word2, long square) {
unordered_map<char, int> char_map;
long switched = 0;
int size = word1.size();
for (int i = size-1; i >= 0; i--) {
int d = square % 10;
char c = word1[i];
if (char_map.find(c) == char_map.end())
char_map[c] = d;
square = floor(square / 10);
}
for (int i = 0; i < size; i++) {
char c = word2[i];
switched += char_map[c] * pow(10, size-i-1);
}
return switched;
}
void problem_98_solution(bool log) {
unordered_map<string, long> pattern_map;
unordered_set<long> pattern_set;
vector<tuple<string, string>> anagram_pairs = get_anagram_pairs("misc/p098_words.txt",
pattern_map,
pattern_set);
unordered_map<long, unordered_set<long>> square_map;
create_square_map(square_map, pattern_set);
long largest = 0;
for (tuple<string, string> pair: anagram_pairs) {
string word1 = get<0>(pair);
string word2 = get<1>(pair);
long pattern1 = pattern_map[word1];
long pattern2 = pattern_map[word2];
for (long square: square_map[pattern1]) {
long switched = switch_digits(word1, word2, square);
if (square_map[pattern2].find(switched) != square_map[pattern2].end()) {
if (switched > largest)
largest = switched;
}
}
for (long square: square_map[pattern2]) {
long switched = switch_digits(word2, word1, square);
if (square_map[pattern1].find(switched) != square_map[pattern1].end()) {
if (switched > largest)
largest = switched;
}
}
}
if (log) cout << largest << endl;
}
#endif //PROJECT_EULER_PROBLEM98_H