-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathString_Hashing
More file actions
248 lines (236 loc) · 5.69 KB
/
String_Hashing
File metadata and controls
248 lines (236 loc) · 5.69 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*Subham Sanghai*/
#include<bits/stdc++.h>
#define mod1 1000000007
#define mod2 1000000009
#define mod3 1000000021
using namespace std;
long long powers(long long a,long long b,long long mod)
{
if(b==0)
return 1;
else if(b%2==0)
{
long long s=powers(a,b/2,mod)%mod;
return (s*s)%mod;
}
else
return (a*powers(a,(b-1),mod))%mod;
}
int main()
{
string s;
cin>>s;
long long l=s.length();
long long starthash1[l+1],starthash2[l+1],starthash3[l+1],endhash1[l+1],endhash2[l+1],endhash3[l+1];
long long val1,val2,val3,start1,start2,start3,end1,end2,end3,length=0;
starthash1[0]=(s[0]-96);
string suffix,prefix;
starthash2[0]=(s[0]-96);
starthash3[0]=(s[0]-96);
for(long long i=1;i<l;i++)
{
starthash1[i]=((starthash1[i-1]*96)%mod1+(s[i]-96))%mod1;
starthash2[i]=((starthash2[i-1]*96)%mod2+(s[i]-96))%mod2;
starthash3[i]=((starthash3[i-1]*96)%mod3+(s[i]-96))%mod3;
}
endhash1[l-1]=(s[l-1]-96);
endhash2[l-1]=(s[l-1]-96);
endhash3[l-1]=(s[l-1]-96);
for(long long i=l-2;i>=0;i--)
{
endhash1[i]=((endhash1[i+1]*96)%mod1+(s[i]-96))%mod1;
endhash2[i]=((endhash2[i+1]*96)%mod2+(s[i]-96))%mod2;
endhash3[i]=((endhash3[i+1]*96)%mod3+(s[i]-96))%mod3;
}
// for(long long i=0;i<l;i++)
// cout<<starthash1[i]<<" "<<endhash1[i]<<"\n";
for(long long i=(l/2);i>=0;i--)
{
//with odd length
if((2*i)<l)
{
start1=starthash1[i];
start2=starthash2[i];
start3=starthash3[i];
val1=0;
val2=0;
val3=0;
if(((2*i)+1)<l)
{
val1=(endhash1[(2*i)+1]*powers(96,(i+1),mod1))%mod1;
val2=(endhash2[(2*i)+1]*powers(96,(i+1),mod2))%mod2;
val3=(endhash3[(2*i)+1]*powers(96,(i+1),mod3))%mod3;
}
end1=(endhash1[i]-val1+mod1)%mod1;
end2=(endhash2[i]-val2+mod2)%mod2;
end3=(endhash3[i]-val3+mod3)%mod3;
if(start1==end1 && start2==end2 && start3==end3)
{
length=(2*i)+1;
}
//cout<<i<<" length1 "<<length<<"\n";
}
//With even length
if((2*(i+1))<=l)
{
start1=starthash1[i];
start2=starthash2[i];
start3=starthash3[i];
val1=0;
val2=0;
val3=0;
if((2*(i+1))<l)
{
val1=(endhash1[(2*(i+1))]*powers(96,(i+1),mod1))%mod1;
val2=(endhash2[(2*(i+1))]*powers(96,(i+1),mod2))%mod2;
val3=(endhash3[(2*(i+1))]*powers(96,(i+1),mod3))%mod3;
}
end1=(endhash1[i+1]-val1+mod1)%mod1;
end2=(endhash2[i+1]-val2+mod2)%mod2;
end3=(endhash3[i+1]-val3+mod3)%mod3;
if(start1==end1 && start2==end2 && start3==end3)
{
length=max(length,(2*(i+1)));
//cout<<i<<" length2 "<<length<<"\n";
}
}
if(length!=0)
{
prefix=s.substr(0,length);
break;
}
}
length=0;
string s2;
s2=s;
reverse(s2.begin(),s2.end());
l=s2.length();
starthash1[0]=(s2[0]-96);
starthash2[0]=(s2[0]-96);
starthash3[0]=(s2[0]-96);
for(long long i=1;i<l;i++)
{
starthash1[i]=((starthash1[i-1]*96)%mod1+(s2[i]-96))%mod1;
starthash2[i]=((starthash2[i-1]*96)%mod2+(s2[i]-96))%mod2;
starthash3[i]=((starthash3[i-1]*96)%mod3+(s2[i]-96))%mod3;
}
endhash1[l-1]=(s2[l-1]-96);
endhash2[l-1]=(s2[l-1]-96);
endhash3[l-1]=(s2[l-1]-96);
for(long long i=l-2;i>=0;i--)
{
endhash1[i]=((endhash1[i+1]*96)%mod1+(s2[i]-96))%mod1;
endhash2[i]=((endhash2[i+1]*96)%mod2+(s2[i]-96))%mod2;
endhash3[i]=((endhash3[i+1]*96)%mod3+(s2[i]-96))%mod3;
}
for(long long i=(l/2);i>=0;i--)
{
//with odd length
if((2*i)<l)
{
start1=starthash1[i];
start2=starthash2[i];
start3=starthash3[i];
val1=0;
val2=0;
val3=0;
if(((2*i)+1)<l)
{
val1=(endhash1[(2*i)+1]*powers(96,(i+1),mod1))%mod1;
val2=(endhash2[(2*i)+1]*powers(96,(i+1),mod2))%mod2;
val3=(endhash3[(2*i)+1]*powers(96,(i+1),mod3))%mod3;
}
end1=(endhash1[i]-val1+mod1)%mod1;
end2=(endhash2[i]-val2+mod2)%mod2;
end3=(endhash3[i]-val3+mod3)%mod3;
if(start1==end1 && start2==end2 && start3==end3)
{
length=(2*i)+1;
}
}
//With even length
if((2*(i+1))<=l)
{
start1=starthash1[i];
start2=starthash2[i];
start3=starthash3[i];
val1=0;
val2=0;
val3=0;
if((2*(i+1))<l)
{
val1=(endhash1[(2*(i+1))]*powers(96,(i+1),mod1))%mod1;
val2=(endhash2[(2*(i+1))]*powers(96,(i+1),mod2))%mod2;
val3=(endhash3[(2*(i+1))]*powers(96,(i+1),mod3))%mod3;
}
end1=(endhash1[i+1]-val1+mod1)%mod1;
end2=(endhash2[i+1]-val2+mod2)%mod2;
end3=(endhash3[i+1]-val3+mod3)%mod3;
if(start1==end1 && start2==end2 && start3==end3)
{
length=max(length,(2*(i+1)));
}
}
if(length!=0)
{
suffix=s2.substr(0,length);
break;
}
}
string newstring=(suffix+prefix);
cout<<newstring<<"\n";
string A=newstring+'$'+newstring;
long long l1=newstring.length();
long long n=A.length();
long long L = 0, R = 0;
long long z[n+1];
fill(z,z+n+1,0);
for (long long i = 1; i < n; i++)
{
if (i > R)
{
L = R = i;
while (R < n && A[R-L] == A[R])
{
R++;
}
z[i] = R-L;
R--;
}
else
{
long long k = i-L;
if (z[k] < R-i+1)
{
z[i] = z[k];
}
else
{
L = i;
while (R < n && A[R-L] == A[R])
{
R++;
}
z[i] = R-L;
R--;
}
}
}
long long count1[l1+2];
fill(count1,count1+l1+2,0);
for(long long i=l1+1;i<n;i++)
{
count1[z[i]]++;//if count[6]=1 then it means prefix of length 1,2,3 ... 6 all are present .so if count1[6]=1 then count of all strings of length 1,2 .. 6 should be increased so we maintained a cum sum
}
long long sum[l1+2];
sum[l1+1]=0;
for(long long i=l1;i>=1;i--)
{
sum[i]=sum[i+1]+count1[i];
}
for(long long i=1;i<=l1;i++)
{
cout<<sum[i]<<" ";
}
cout<<"\n";
}