-
Notifications
You must be signed in to change notification settings - Fork 111
/
check-if-an-original-string-exists-given-two-encoded-strings.cc
94 lines (89 loc) · 2.39 KB
/
check-if-an-original-string-exists-given-two-encoded-strings.cc
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
// Check if an Original String Exists Given Two Encoded Strings
const int P = 32768;
struct HNode {int key; bool val; HNode *next; } pool[P*2], *head[P], *allo;
class Solution {
static const int BASE = 999*10;
string s1, s2;
bool f(int i, int j, int p) {
int key = ((p+BASE)*41+i)*41+j, hash = key%P;
for (HNode *x = head[hash]; x; x = x->next)
if (x->key == key)
return x->val;
if (s1.size() == i && s2.size() == j) return !p;
bool ret = false;
if (unsigned(s1[i]-'0') < 10) {
int x = 0;
do {
x = x*10+s1[i]-'0';
ret = f(++i, j, p+x);
} while (!ret && unsigned(s1[i]-'0') < 10);
} else if (unsigned(s2[j]-'0') < 10) {
int x = 0;
do {
x = x*10+s2[j]-'0';
ret = f(i, ++j, p-x);
} while (!ret && unsigned(s2[j]-'0') < 10);
} else {
if (p > 0) {
if (j < s2.size())
ret = f(i, j+1, p-1);
} else if (p < 0) {
if (i < s1.size())
ret = f(i+1, j, p+1);
} else if (s1[i] == s2[j])
ret = f(i+1, j+1, p);
}
HNode *x = allo == end(pool) ? new HNode : allo++;
*x = {key, ret, head[hash]};
head[hash] = x;
return ret;
}
public:
bool possiblyEquals(string s1, string s2) {
this->s1 = move(s1);
this->s2 = move(s2);
allo = pool;
fill_n(head, sizeof(head)/sizeof(*head), nullptr);
return f(0, 0, 0);
}
};
/// unordered_map (slow)
class Solution {
static const int BASE = 999*10;
string s1, s2;
unordered_map<unsigned, char> memo;
bool f(int i, int j, int p) {
char &ret = memo[((p+BASE)*41+i)*41+j];
if (ret) return ret-1;
if (s1.size() == i && s2.size() == j) return !p;
if (unsigned(s1[i]-'0') < 10) {
int x = 0;
do {
x = x*10+s1[i]-'0';
ret = f(++i, j, p+x);
} while (!ret && unsigned(s1[i]-'0') < 10);
} else if (unsigned(s2[j]-'0') < 10) {
int x = 0;
do {
x = x*10+s2[j]-'0';
ret = f(i, ++j, p-x);
} while (!ret && unsigned(s2[j]-'0') < 10);
} else {
if (p > 0) {
if (j < s2.size())
ret = f(i, j+1, p-1);
} else if (p < 0) {
if (i < s1.size())
ret = f(i+1, j, p+1);
} else if (s1[i] == s2[j])
ret = f(i+1, j+1, p);
}
return ret++;
}
public:
bool possiblyEquals(string s1, string s2) {
this->s1 = s1;
this->s2 = s2;
return f(0, 0, 0);
}
};