-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCyclic shift.cpp
348 lines (301 loc) · 11 KB
/
Cyclic shift.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
Solution by Rahul Surana
***********************************************************
A large binary number is represented by a string of size A and N comprises of 0s and 1s.
You must perform a cyclic shift on this string. The cyclic shift operation is defined as follows:
If the string A is [a0,a1,a2,...,a(n-1)], then after performing one cyclic shift, the string becomes [a1,a2,a3,...,a0].
You performed the shift infinite number of times and each time you recorded the value of the binary number represented by the string.
The maximum binary number formed after performing (possibly 0) the operation is B.
Your task is to determine the number of cyclic shifts
that can be performed such that the value represented by the string A will be equal to B for the kth time.
Input format
First line: A single integer T denoting the number of test cases
For each test case:
First line: Two space-separated integers N and K
Second line: A denoting the string
***********************************************************
*/
#include <bits/stdc++.h>
#define ll unsigned long long int
#define vl vector<ll>
#define vi vector<int>
#define pi pair<int,int>
#define pl pair<ll,ll>
#define all(a) a.begin(),a.end()
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i,a) for(int i = 0; i < a; i++)
#define trace(x) cerr<<#x<<" : "<<x<<endl;
#define trace2(x,y) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
#define trace3(x,y,z) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<" | "<<#z<<" : "<<z<<endl;
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
vector<int> z_function(string s)
{
int n = (int) s.length();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r)
z[i] = min (r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]])
++z[i];
if (i + z[i] - 1 > r)
l = i, r = i + z[i] - 1;
}
return z;
}
vector<int> sort_cyclic_shifts(string const& s)
{
int n = s.size();
const int alphabet = 256;
vector<int> p(n), c(n), cnt(max(alphabet, n), 0);
for (int i = 0; i < n; i++)
cnt[s[i]]++;
for (int i = 1; i < alphabet; i++)
cnt[i] += cnt[i-1];
for (int i = 0; i < n; i++)
p[--cnt[s[i]]] = i;
c[p[0]] = 0;
int classes = 1;
for (int i = 1; i < n; i++) {
if (s[p[i]] != s[p[i-1]])
classes++;
c[p[i]] = classes - 1;
}
vector<int> pn(n), cn(n);
for(int h = 0; (1 << h) < n; ++h) {
for (int i = 0; i < n; i++) {
pn[i] = p[i] - (1 << h);
if (pn[i] < 0)
pn[i] += n;
}
fill(cnt.begin(), cnt.begin() + classes, 0);
for (int i = 0; i < n; i++)
cnt[c[pn[i]]]++;
for (int i = 1; i < classes; i++)
cnt[i] += cnt[i-1];
for (int i = n-1; i >= 0; i--)
p[--cnt[c[pn[i]]]] = pn[i];
cn[p[0]] = 0;
classes = 1;
for (int i = 1; i < n; i++) {
pair<int, int> cur = {c[p[i]], c[(p[i] + (1 << h)) % n]};
pair<int, int> prev = {c[p[i-1]], c[(p[i-1] + (1 << h)) % n]};
if (cur != prev)
++classes;
cn[p[i]] = classes - 1;
}
c.swap(cn);
}
return p;
}
string flip(string s)
{
string a;
for(ll i=0;i<s.length();i++)
{
if(s[i] == '0')
a += '1';
else
a += '0';
}
return a;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--)
{
string s;
ll n,p;
cin>>n>>p;
cin>>s;
s = flip(s);
vector<int> z = z_function(s);
// for(ll i = 0; i < z.size(); i++){ cout << z[i]<<" ";}
ll period=n;
for(ll i=1;i<n;i++)
{
if(i+z[i] == n && n%i == 0)
{
period = i;
break;
}
}
string A = s.substr(0,period);
vector<int> v = sort_cyclic_shifts(A);
ll ans = v[0] + (p-1)*period;
cout<<ans<<"\n";
}
return 0;
}
// <------------------------------------END-------------------------------------------> \\
// #include <bits/stdc++.h>
// #define ll unsigned long long int
// #define vl vector<ll>
// #define vi vector<int>
// #define pi pair<int,int>
// #define pl pair<ll,ll>
// #define all(a) a.begin(),a.end()
// #define mem(a,x) memset(a,x,sizeof(a))
// #define pb push_back
// #define mp make_pair
// #define F first
// #define S second
// #define FOR(i,a) for(int i = 0; i < a; i++)
// #define trace(x) cerr<<#x<<" : "<<x<<endl;
// #define trace2(x,y) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
// #define trace3(x,y,z) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<" | "<<#z<<" : "<<z<<endl;
// #define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
// using namespace std;
// ll binaryToDecimal(string n)
// {
// string num = n;
// ll dec_value = 0;
// // Initializing base value to 1, i.e 2^0
// ll base = 1;
// ll len = num.length();
// for (int i = len - 1; i >= 0; i--) {
// if (num[i] == '1')
// dec_value += base;
// base = base * 2;
// }
// return dec_value;
// }
// void solve() {
// vector<int> index;
// ll n,k;
// cin >> n >> k;
// string s,copy;
// cin >> s;
// // copy2 = s;
// copy = s;
// // cout << s << "\n";
// for(ll i=0;i<n;i++){
// // cout << "stoi(s) "<<stoi(s)<<" ";
// // ll a = binaryToDecimal(s);
// if(s >= copy){
// if ((bool)(s==copy)){
// index.push_back(i);
// }
// else { index.clear(); copy = s; index.push_back(i);}
// // m = a;
// // cout << "copy "<<copy<<" ";
// // cout << "a :" << a<<" ";
// }
// reverse(s.begin()+1, s.end());
// reverse(s.begin(), s.end());
// }
// cout << "copy "<< s<< "\n";
// // cout <<"after the max"<< s << "\n";
// // for (ll i = 0; i < n; i++){
// // if ((bool)(s==copy)){
// // index.push_back(i);
// // }
// // reverse(s.begin()+1, s.end());
// // reverse(s.begin(), s.end());
// // }
// // cout << s << "\n";
// for (ll i = 0; i < index.size(); i++){ cout << index[i]<<" ";}
// ll x = (int)k/index.size();
// int y = k%index.size();
// cout <<"\n"<< index.size() <<" "<< x-1 <<" "<< n <<" "<< (x-1)*n<< " " << y <<"\n";
// if(x==0) cout << index[y-1] <<"\n";
// else if(y==0 && x!=0) cout <<((x-1)*n + index[index.size()-1]) << "\n";
// else cout << (x)*n + index[y-1] << "\n";
// }
// int tc;
// int main() {
// fast_io;
// cin >> tc;
// for (int t = 1; t <= tc; t++) {
// solve();
// }
// }
/*#include <bits/stdc++.h>
#define ll unsigned long long int
#define vl vector<ll>
#define vi vector<int>
#define pi pair<int,int>
#define pl pair<ll,ll>
#define all(a) a.begin(),a.end()
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i,a) for(int i = 0; i < a; i++)
#define trace(x) cerr<<#x<<" : "<<x<<endl;
#define trace2(x,y) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
#define trace3(x,y,z) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<" | "<<#z<<" : "<<z<<endl;
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
string maxone(string s){
string temp=s+s;
int p =0,mo=0,count=0;
string ans = "";
for(ll i=0; i < 2*s.length();i++){
if(s[i%s.length()]=='1') count++;
else count = 0;
if(count>=mo && s.substr((s.length()+p-mo+1)%s.length())+ s.substr(0,(s.length()+p-mo+1)%s.length())>ans) {mo=count; p = i%s.length(); ans = s.substr((s.length()+p-mo+1)%s.length())+ s.substr(0,(s.length()+p-mo+1)%s.length());}
}
return ans;
}
void solve() {
vector<int> index;
ll n,k;
cin >> n >> k;
string s,copy;
cin >> s;
// copy2 = s;
copy = maxone(s);
// cout << s << "\n";
// for(ll i=0;i<n;i++){
// if(s >= copy){
// if ((bool)(s>copy)){
// index.clear();
// index.pb(i);
// copy = s;
// }
// else { index.pb(i);}
// // m = a;
// // cout << "copy "<<copy<<" ";
// // cout << "a :" << a<<" ";
// }
// reverse(s.begin()+1, s.end());
// reverse(s.begin(), s.end());
// }
// cout << "copy "<< s<< "\n";
// cout <<"after the max"<< s << "\n";
// index.push_back(0);
for (ll i = 0; i < n; i++){
if ((bool)(s==copy)){
index.push_back(i);
}
reverse(s.begin()+1, s.end());
reverse(s.begin(), s.end());
}
// cout << s << "\n";
// for (ll i = 0; i < index.size(); i++){ cout << index[i]<<" ";}
int x = k / index.size();
int y = k % index.size();
// cout <<"\n"<< index.size() <<" "<< x <<" "<< n <<" "<< (x)*n<< " " << y <<"\n";
if(x==0) cout << index[y-1] <<"\n";
else if(y==0 && x!=0) cout <<((x-1)*n + index[index.size()-1]) << "\n";
else cout << (x)*n + index[y-1] << "\n";
}
int tc;
int main() {
fast_io;
cin >> tc;
for (int t = 1; t <= tc; t++) {
solve();
// cout << maxone("1111101110111111100111111111101111111011111010101011111111111011011110101111111111010111111110111111111111101111111011110111111111111101011110111111011111111011100111111111111111111110110111111001111101111101110100101100111111111111011110111111111110011110100110111111111101111100111011111111101111111111111110111111101111010010110111110100001101111011100011111101011111011011111111111000101011011111001111111111111111111111000111111111111011111111111111110110011111101111010111111111111110111111111011111111010111111110111001111111111111111000010111101111110110111111111111110111011111111111111110110111111111111111110111011101111010111111111111011111111111101111011111111011111011011111111111101111100011111111111011111011110111110111111111111111111101110111111110100111111101111110111010111111111111111111111101111011100111111101111111011111101101111110111001111100111111111111011110111011011111111111111111011111111101101101111011110101101100111110111110111011011111110110110101111111111110100111");
}
}*/