forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprague_Grundy_Theorem.cpp
151 lines (139 loc) · 3.36 KB
/
Sprague_Grundy_Theorem.cpp
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
/*The game used to implement Sprague-Grundy is defined below
-> There are 2 people playing this composite impartial game
-> The second player writes an arbitrary string .
-> Then each player starting with the first player starts
removing a continous substring which exists in a list of
substrings that are possible to remove.
-> The first player who can't remove a substring loses. */
#include <bits/stdc++.h>
using namespace std;
// Macros used
typedef pair<int, int> pii;
#define maxs 33
#define INF 1e9
// Global Variables
int t, y, st[maxs][maxs], pc[maxs][maxs], kk[maxs][maxs];
string a, wq[maxs];
void process(int x)
{
// Process the Grundy numbers for each input
int g, h;
pc[x][0] = -1;
for (g = 0, h = -1; g < wq[x].size();)
{
while (h >= 0 && wq[x][g] != wq[x][h])
{
h = pc[x][h];
}
g++, h++;
pc[x][g] = h;
}
}
vector<pii> kmp(int x, string s)
{
int g, h;
vector<pii> all;
for (g = 0, h = 0; g < s.size();)
{
while (h >= 0 && s[g] != wq[x][h])
{
h = pc[x][h];
}
g++, h++;
if (h == wq[x].size())
{
all.push_back(pii(g - h, g));
h = pc[x][h];
}
}
return all;
}
// Sprague Grundy Theorem implemented
/*Note that after a player erases a substring S(a, b),
in S(i, j), the game is split into two subgames, S(i, a-1)
and S(b+1, j). Because a player can choose any game in his/her
turn, the Grundy number of the game is simply the XOR of the
Grundy numbers of the two subgames. */
int sprague_grundy(int nx, int ny)
{
if (nx > ny)
return 0;
if (kk[nx][ny] == t)
return st[nx][ny];
int g, h, px, py, xr1, xr2, now;
string s;
vector<int> ss;
vector<pii> all;
for (g = nx; g <= ny; g++)
{
s += a[g];
}
for (g = 0; g < y; g++)
{
all = kmp(g, s);
for (h = 0; h < all.size(); h++)
{
px = all[h].first, py = all[h].second;
xr1 = (sprague_grundy(nx, nx + px - 1));
xr2 = (sprague_grundy(nx + py, ny));
ss.push_back(xr1 ^ xr2);
}
}
sort(ss.begin(), ss.end());
ss.erase(unique(ss.begin(), ss.end()), ss.end());
/*A game is losing iff its Grundy number is zero.
So the first player, will win if G(1, |S|) is not zero.
On the other hand, player 2 will win if G(1, |S|) is zero. */
for (g = now = 0; g < ss.size(); g++, now++)
{
if (ss[g] != now)
break;
}
st[nx][ny] = now;
kk[nx][ny] = t;
return now;
}
/*Let G(i, j) be the Grundy number of the substring
S(i, j). Then G(i, j) = mex({G(i, a-1) XOR G(b+1, j) : S(a, b)
exists in the dictionary and is a substring of S(i, j)} */
int main()
{
int g, h, re;
// Intitialising the array with value of INF
for (g = 0; g <= 30; g++)
{
for (h = 0; h <= 30; h++)
{
kk[g][h] = INF;
}
}
cout << "Enter Main String : ";
cin >> a;
cout << "Enter total number of substrings : ";
cin >> y;
cout << "Enter all " << y << " substrings : " << endl;
for (g = 0; g < y; g++)
{
cin >> wq[g];
process(g);
}
// Run the Sprague Grundy algo on each recursive combination
re = sprague_grundy(0, a.size() - 1);
// Outputting which player wins
if (re == 1)
cout << "Player 1 wins.";
else
cout << "Player 2 wins.";
return 0;
}
/*
Time Complexity: O(n^2)
Sample Input:
Enter main string : spraguegrundy
Enter total number of substrings : 2
Enter all 2 substrings :
spra
grundy
Sample Output:
Player 2 wins.
*/