forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
77 lines (70 loc) · 1.81 KB
/
E.cpp
File metadata and controls
77 lines (70 loc) · 1.81 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
#include <bits/stdc++.h>
using namespace std;
const int BASE = 997;
vector <int> win[55];
vector < pair<long long,int> > a[55];
long long p[55];
int isLose(int len, long long h)
{
int id = lower_bound(a[len].begin(), a[len].end(), make_pair(h, 0)) - a[len].begin();
return id < a[len].size() && a[len][id].first == h && !win[len][id];
}
int main()
{
freopen("game.in", "r", stdin);
freopen("game.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int m;
string s;
cin >> m;
while (m--)
{
cin >> s;
for (int j = 0; j < s.size(); j++)
{
long long h = 0;
for (int k = j; k < s.size(); k++)
{
h = h * BASE + s[k];
a[k - j + 1].push_back({h, k - j + 1 == s.size()});
}
}
}
for (int i = 0; i <= 50; i++)
p[i] = i ? p[i - 1] * BASE : 1;
a[0].push_back({0, 0});
for (int len = 50; len >= 0; len--)
{
sort(a[len].begin(), a[len].end());
int n = 1;
for (int i = 1; i < a[len].size(); i++)
if (a[len][i].first == a[len][n - 1].first) a[len][n - 1] = a[len][i];
else a[len][n++] = a[len][i];
a[len].resize(n);
for (int i = 0; i < n; i++)
if (a[len][i].second) win[len].push_back(1);
else
{
win[len].push_back(0);
for (char c = 33; c <= 126; c++)
{
long long hLeft = c * p[len] + a[len][i].first;
long long hRight = a[len][i].first * BASE + c;
if (isLose(len + 1, hLeft) || isLose(len + 1, hRight))
{
win[len][i] = 1;
break;
}
}
}
}
if (win[0][0])
{
cout << "First" << endl;
for (int i = 0; i < a[1].size(); i++)
if (!win[1][i])
cout << char(a[1][i].first);
}
else cout << "Second" << endl;
}