Skip to content

Commit dcbc156

Browse files
committed
Treaps and Hash Tables Added
1 parent aa3d51c commit dcbc156

10 files changed

+2663
-1
lines changed

Algorithm/63 String Hashing.cpp

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
2+
/** Which of the favors of your Lord will you deny ? **/
3+
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
7+
#define LL long long
8+
#define PII pair<int,int>
9+
#define PLL pair<LL,LL>
10+
#define F first
11+
#define S second
12+
13+
#define ALL(x) (x).begin(), (x).end()
14+
#define READ freopen("alu.txt", "r", stdin)
15+
#define WRITE freopen("vorta.txt", "w", stdout)
16+
17+
#ifndef ONLINE_JUDGE
18+
#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
19+
#else
20+
#define DBG(x)
21+
#define endl "\n"
22+
#endif
23+
24+
template<class T1, class T2>
25+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
26+
template <class T>
27+
ostream &operator <<(ostream &os, vector<T>&v);
28+
template <class T>
29+
ostream &operator <<(ostream &os, set<T>&v);
30+
31+
inline void optimizeIO()
32+
{
33+
ios_base::sync_with_stdio(false);
34+
cin.tie(NULL);
35+
}
36+
37+
const int nmax = 2e5+7;
38+
const int prime = 31;
39+
const int mod = 1e9+7;
40+
41+
int p[nmax];
42+
int prefixHash[nmax];
43+
44+
/*
45+
* Modular Arithmetic
46+
* */
47+
48+
int MUL(int a,int b)
49+
{
50+
a %= mod;
51+
b %= mod;
52+
return (1LL*a*b) % mod;
53+
}
54+
55+
int ADD(int a,int b)
56+
{
57+
a %= mod;
58+
b %= mod;
59+
return (1LL*a+b) % mod;
60+
}
61+
62+
int MINUS(int a,int b)
63+
{
64+
a %= mod;
65+
b %= mod;
66+
return (1LL*a-b+mod)%mod;
67+
}
68+
69+
void precalc()
70+
{
71+
p[0] = 1;
72+
for(int i=1;i<nmax;i++) p[i] = MUL(p[i-1],prime);
73+
}
74+
75+
void computePrefixHash(string s) /*prefix hash function*/
76+
{
77+
int n = s.size();
78+
s = "#" + s;
79+
80+
for(int i=1;i<=n;i++) prefixHash[i] = ADD(prefixHash[i-1],MUL(s[i]-'a'+1,p[i-1]));
81+
}
82+
83+
int getSubstringHash(int l,int r) /*1 based indexing*/
84+
{
85+
int ret = MINUS(prefixHash[r],MUL(prefixHash[l-1],p[r-l+1])); /*substring hash from prefix hash*/
86+
return ret;
87+
}
88+
89+
int32_t main()
90+
{
91+
optimizeIO();
92+
93+
precalc(); /*precalculating prime exponentiations*/
94+
95+
string s;
96+
cin>>s;
97+
98+
computePrefixHash(s);
99+
100+
int n = s.size();
101+
s = "#" + s;
102+
103+
return 0;
104+
}
105+
106+
/**
107+
108+
**/
109+
110+
template<class T1, class T2>
111+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
112+
{
113+
os<<"{"<<p.first<<", "<<p.second<<"} ";
114+
return os;
115+
}
116+
template <class T>
117+
ostream &operator <<(ostream &os, vector<T>&v)
118+
{
119+
os<<"[ ";
120+
for(T i:v)
121+
{
122+
os<<i<<" " ;
123+
}
124+
os<<" ]";
125+
return os;
126+
}
127+
128+
template <class T>
129+
ostream &operator <<(ostream &os, set<T>&v)
130+
{
131+
os<<"[ ";
132+
for(T i:v)
133+
{
134+
os<<i<<" ";
135+
}
136+
os<<" ]";
137+
return os;
138+
}

Data Structure/35 Implicit Treap.cpp

+229
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
2+
/** Which of the favors of your Lord will you deny ? **/
3+
4+
#include<bits/stdc++.h>
5+
#include <ostream>
6+
7+
using namespace std;
8+
9+
#define LL long long
10+
#define PII pair<int,int>
11+
#define PLL pair<LL,LL>
12+
#define F first
13+
#define S second
14+
15+
#define ALL(x) (x).begin(), (x).end()
16+
#define READ freopen("alu.txt", "r", stdin)
17+
#define WRITE freopen("vorta.txt", "w", stdout)
18+
19+
#ifndef ONLINE_JUDGE
20+
#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
21+
#else
22+
#define DBG(x)
23+
#define endl "\n"
24+
#endif
25+
26+
template<class T1, class T2>
27+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
28+
template <class T>
29+
ostream &operator <<(ostream &os, vector<T>&v);
30+
template <class T>
31+
ostream &operator <<(ostream &os, set<T>&v);
32+
33+
inline void optimizeIO()
34+
{
35+
ios_base::sync_with_stdio(false);
36+
cin.tie(NULL);
37+
}
38+
39+
const int nmax = 2e5+7;
40+
41+
#define int long long
42+
43+
/*
44+
* Implicit Treap
45+
* ==============
46+
* 0 based indexing
47+
* */
48+
49+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
50+
int getrand(int a, int b)
51+
{
52+
int x = uniform_int_distribution<int>(a, b)(rng);
53+
return x;
54+
}
55+
56+
struct Node
57+
{
58+
int data,prior,subTreeSize;
59+
Node* l;
60+
Node* r;
61+
62+
// extra variables to solve problems , change here
63+
64+
Node(int data)
65+
{
66+
this->data = data;
67+
prior = getrand(-2e9, 2e9);
68+
subTreeSize = 1;
69+
70+
l = r = NULL;
71+
72+
// change here
73+
}
74+
75+
friend ostream &operator<<(ostream &os, const Node &node) {
76+
os << "data: " << node.data << " prior: " << node.prior << " subTreeSize: " << node.subTreeSize;
77+
return os;
78+
}
79+
};
80+
typedef Node* Treap;
81+
82+
struct ImplicitTreap
83+
{
84+
Treap root;
85+
86+
ImplicitTreap()
87+
{
88+
root = NULL;
89+
}
90+
91+
int sz(Treap t)
92+
{
93+
return t == NULL ? 0 : t->subTreeSize;
94+
}
95+
96+
void update_sz(Treap t)
97+
{
98+
if(t) t->subTreeSize = 1 + sz(t->l) + sz(t->r);
99+
}
100+
101+
/*Add
102+
* 1. reset
103+
* 2. combine
104+
* 3. push
105+
* 4. segmentTreeOperation
106+
* */
107+
108+
void split(Treap t,Treap &l,Treap &r,int pos,int add = 0)
109+
{
110+
if(!t)
111+
{
112+
l = NULL;
113+
r = NULL;
114+
return;
115+
}
116+
117+
int implicit_pos = add + sz(t->l); /*implicit indexing in implicit treap*/
118+
119+
if(implicit_pos <= pos)
120+
{
121+
split(t->r,t->r,r,pos,implicit_pos + 1);
122+
l = t;
123+
}
124+
else
125+
{
126+
split(t->l,l,t->l,pos,add);
127+
r = t;
128+
}
129+
130+
update_sz(t); /*update size*/
131+
}
132+
133+
void meld(Treap &ret,Treap l,Treap r)
134+
{
135+
136+
if(!l || !r)
137+
{
138+
if(l) ret = l;
139+
if(r) ret = r;
140+
}
141+
else if(l->prior > r->prior)
142+
{
143+
meld(l->r,l->r,r);
144+
ret = l;
145+
}
146+
else
147+
{
148+
meld(r->l,l,r->l);
149+
ret = r;
150+
}
151+
152+
update_sz(ret); /*update size*/
153+
}
154+
155+
void print(Treap t,int add)
156+
{
157+
if(!t) return;
158+
159+
cout<<*t<<endl;
160+
161+
print(t->l,add);
162+
print(t->r,add + sz(t->l) + 1);
163+
}
164+
165+
void print()
166+
{
167+
print(root,0);
168+
}
169+
170+
// operations
171+
172+
void insert(int val)
173+
{
174+
Treap newTreap = new Node(val);
175+
meld(root,root,newTreap);
176+
}
177+
};
178+
179+
180+
int32_t main()
181+
{
182+
optimizeIO();
183+
184+
int n,q;
185+
cin>>n>>q;
186+
187+
ImplicitTreap t;
188+
189+
vector<int>v(n+1);
190+
for(int i=1;i<=n;i++) cin>>v[i] , t.insert(v[i]);
191+
192+
// t.print();
193+
194+
return 0;
195+
}
196+
197+
/**
198+
199+
**/
200+
201+
template<class T1, class T2>
202+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
203+
{
204+
os<<"{"<<p.first<<", "<<p.second<<"} ";
205+
return os;
206+
}
207+
template <class T>
208+
ostream &operator <<(ostream &os, vector<T>&v)
209+
{
210+
os<<"[ ";
211+
for(T i:v)
212+
{
213+
os<<i<<" " ;
214+
}
215+
os<<" ]";
216+
return os;
217+
}
218+
219+
template <class T>
220+
ostream &operator <<(ostream &os, set<T>&v)
221+
{
222+
os<<"[ ";
223+
for(T i:v)
224+
{
225+
os<<i<<" ";
226+
}
227+
os<<" ]";
228+
return os;
229+
}

0 commit comments

Comments
 (0)