-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgp_hashTable.cpp
More file actions
222 lines (195 loc) · 7.14 KB
/
gp_hashTable.cpp
File metadata and controls
222 lines (195 loc) · 7.14 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
⡆⣐⢕⢕⢕⢕⢕⢕⢕⢕⠅⢗⢕⢕⢕⢕⢕⢕⢕⠕⠕⢕⢕⢕⢕⢕⢕⢕⢕⢕
⢐⢕⢕⢕⢕⢕⣕⢕⢕⠕⠁⢕⢕⢕⢕⢕⢕⢕⢕⠅⡄⢕⢕⢕⢕⢕⢕⢕⢕⢕
⢕⢕⢕⢕⢕⠅⢗⢕⠕⣠⠄⣗⢕⢕⠕⢕⢕⢕⠕⢠⣿⠐⢕⢕⢕⠑⢕⢕⠵⢕
⢕⢕⢕⢕⠁⢜⠕⢁⣴⣿⡇⢓⢕⢵⢐⢕⢕⠕⢁⣾⢿⣧⠑⢕⢕⠄⢑⢕⠅⢕
⢕⢕⠵⢁⠔⢁⣤⣤⣶⣶⣶⡐⣕⢽⠐⢕⠕⣡⣾⣶⣶⣶⣤⡁⢓⢕⠄⢑⢅⢑
⠍⣧⠄⣶⣾⣿⣿⣿⣿⣿⣿⣷⣔⢕⢄⢡⣾⣿⣿⣿⣿⣿⣿⣿⣦⡑⢕⢤⠱⢐
⢠⢕⠅⣾⣿⠋⢿⣿⣿⣿⠉⣿⣿⣷⣦⣶⣽⣿⣿⠈⣿⣿⣿⣿⠏⢹⣷⣷⡅⢐
⣔⢕⢥⢻⣿⡀⠈⠛⠛⠁⢠⣿⣿⣿⣿⣿⣿⣿⣿⡀⠈⠛⠛⠁⠄⣼⣿⣿⡇⢔
⢕⢕⢽⢸⢟⢟⢖⢖⢤⣶⡟⢻⣿⡿⠻⣿⣿⡟⢀⣿⣦⢤⢤⢔⢞⢿⢿⣿⠁⢕
⢕⢕⠅⣐⢕⢕⢕⢕⢕⣿⣿⡄⠛⢀⣦⠈⠛⢁⣼⣿⢗⢕⢕⢕⢕⢕⢕⡏⣘⢕
⢕⢕⠅⢓⣕⣕⣕⣕⣵⣿⣿⣿⣾⣿⣿⣿⣿⣿⣿⣿⣷⣕⢕⢕⢕⢕⡵⢀⢕⢕
⢑⢕⠃⡈⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢃⢕⢕⢕
⣆⢕⠄⢱⣄⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⢁⢕⢕⠕⢁
⣿⣦⡀⣿⣿⣷⣶⣬⣍⣛⣛⣛⡛⠿⠿⠿⠛⠛⢛⣛⣉⣭⣤⣂⢜⠕⢑⣡⣴⣿
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // this is for the gp_hash_table
using namespace std;
typedef long long LL;
typedef long double LD;
#define MAX (5002)
// #define MAX 10
#define pi acos(-1)
#define whatis(x) cout << #x << " is " << x << "\n";
#define all(x) x.begin(), x.end()
#define endl "\n"
#define DEB cout << "TESTING" NL
#define TESTS int test; cin>>test; for(int kase = 1 ; kase <= test ; kase++) solve(kase);
#define CASEOUT cout << "Case " << testcase << ": " <<
#define YESNO cout << (possible? "YES\n" : "NO\n");
// #define YES cout << "YES\n";
// #define NO cout << "NO\n";
#define int long long
#define pii pair<int,int>
namespace Hashing {
#define ff first
#define ss second
const pii M = {1e9+7, 1e9+9}; ///Should be large primes
const LL base = 1259; ///Should be larger than alphabet size
const int N = 1e6 + 2; ///Highest length of string
pii operator+ (const pii& a, LL x) {return {a.ff + x, a.ss + x};}
pii operator- (const pii& a, LL x) {return {a.ff - x, a.ss - x};}
pii operator* (const pii& a, LL x) {return {a.ff * x, a.ss * x};}
pii operator+ (const pii& a, pii x) {return {a.ff + x.ff, a.ss + x.ss};}
pii operator- (const pii& a, pii x) {return {a.ff - x.ff, a.ss - x.ss};}
pii operator* (const pii& a, pii x) {return {a.ff * x.ff, a.ss * x.ss};}
pii operator% (const pii& a, pii m) {return {a.ff % m.ff, a.ss % m.ss};}
ostream& operator<<(ostream& os, pii hash) {
return os<<"("<<hash.ff<<", "<<hash.ss<<")";
}
pii pb[N]; ///powers of base mod M
///Call pre before everything
void hashPre() {
pb[0] = {1,1};
for (int i=1; i<N; i++) pb[i] = (pb[i-1] * base)%M;
}
///Calculates hashes of all prefixes of s including empty prefix
vector<pii> hashList(string s) {
int n = s.size();
vector<pii> ans(n+1);
ans[0] = {0,0};
for (int i=1; i<=n; i++) ans[i] = (ans[i-1] * base + s[i-1])%M;
return ans;
}
///Calculates hash of substring s[l..r] (1 indexed)
pii substringHash(const vector<pii> &hashlist, int l, int r) {
return (hashlist[r]+(M-hashlist[l-1])*pb[r-l+1])%M;
}
///Calculates Hash of a string
pii Hash (string s) {
pii ans = {0,0};
for (int i=0; i<s.size(); i++) ans=(ans*base + s[i])%M;
return ans;
}
///Tested on https://toph.co/p/palindromist
///appends c to string
pii append(pii cur, char c) {
return (cur*base + c)%M;
}
///Tested on https://toph.co/p/palindromist
///prepends c to string with size k
pii prepend(pii cur, int k, char c) {
return (pb[k]*c + cur)%M;
}
///Tested on https://toph.co/p/chikongunia
///replaces the i-th (0-indexed) character from right from a to b;
pii replace(pii cur, int i, char a, char b) {
return cur + pb[i] * (M+b-a)%M;
}
///Erases c from front of the string with size len
pii pop_front(pii hash, int len, char c) {
return (hash + pb[len-1]*(M-c))%M;
}
///Tested on https://toph.co/p/palindromist
///concatenates two strings where length of the right is k
pii concat(pii left, pii right, int k) {
return (left*pb[k] + right)%M;
}
pii power (const pii& a, LL p) {
if (p==0) return {1,1};
pii ans = power(a, p/2);
ans = (ans * ans)%M;
if (p%2) ans = (ans*a)%M;
return ans;
}
pii inverse(pii a) {
if (M.ss == 1) return power(a, M.ff-2);
return power(a, (M.ff-1)*(M.ss-1)-1);
}
///Erases c from the back of the string
pii invb = inverse({base, base});
pii pop_back(pii hash, char c) {
return ((hash-c+M)*invb)%M;
}
///Tested on https://toph.co/p/palindromist
///Calculates hash of string with size len repeated cnt times
///This is O(log n). For O(1), pre-calculate inverses
pii repeat(pii hash, int len, LL cnt) {
pii mul = ((pb[len*cnt]-1+M) * inverse(pb[len]-1+M))%M;
pii ans = (hash*mul);
if (pb[len].ff == 1) ans.ff = hash.ff*cnt;
if (pb[len].ss == 1) ans.ss = hash.ss*cnt;
return ans%M;
}
}
using namespace Hashing;
// this following lines of code is for gp-hashtable that is faster than unordered map
// remember to include the file first (at the top)
using namespace __gnu_pbds;
gp_hash_table<int, int> table_for_int_to_int; // use similar to unordered map
// for storing pairs of int, use following hash and declaration
struct chash {
int operator()(pii x) const { return x.first* 31 + x.second; }
};
gp_hash_table<pii, int, chash> table_for_pair_to_int;
////////////////////////////////////////////
string inp;
vector <pii> myHashList;
int position_of_ans;
int ans = 0;
bool check(int len)
{
gp_hash_table <pii, bool, chash> exists;
// unordered_map <pii, int, chash> exists;
// map <pii, int> exists;
pii hashV;
for(int i = 0 ; i + len - 1 < inp.size() ; i++)
{
hashV = substringHash(myHashList, i+1, i+len);
if(exists[hashV] != false)
{
position_of_ans = i;
ans = len;
return true;
}
exists[hashV] = true;
}
return false;
}
int solve(int testcase)
{
hashPre();
cin >> inp;
myHashList = hashList(inp);
int l = 0, h = inp.size()-1;
while(l <= h)
{
int mid = (h + l) / 2;
if(check(mid))
l = mid+1;
else
h = mid-1;
}
if(ans == 0)
{
cout << -1 << endl;
return 0;
}
for(int i = position_of_ans ; i < position_of_ans + ans ; i++)
cout << inp[i];
cout << endl;
return 0;
}
signed main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// cout.tie(NULL);
solve(0);
//TESTS
return 0;
}