output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1e9+7,p=2,N=2e6;
LL T,n,k;
LL poww[32],pre[N],num[N],vis[N];
char s[N];
LL hashp(LL l,LL r)
{
return (pre[r]-pre[l-1]*poww[r-l+1]%mod+mod)%mod;
}
void init()
{
poww[0]=1;
for(int i=1; i<=27; i++) poww[i]=poww[i-1]<<1;
}
int main()
{
init();
cin>>T;
for(LL ca=1; ca<=T; ca++)
{
cin>>n>>k>>(s+1);
for(LL i=1,ar; i<=n; i++)
{
ar=s[i]-'0';
ar^=1;
pre[i]=(pre[i-1]*p+ar)%mod;
num[i]=num[i-1]+ar;
}
LL limit=poww[min((LL)21,k)]-1;
for(LL i=1; i+k-1<=n; i++)
{
LL x=hashp(max(i,i+k-20),i+k-1);
if(k > 20 && num[i + k - 20] - num[i - 1] > 0) continue;// 超过了 1e6 ,20位以上 有 1
if(x > n) continue; //超过 n 没必要记录
vis[x]=ca;
}
LL x=-1;
for(LL i=0; i<=limit&&x==-1; i++)
{
if(vis[i]!=ca) x=i;
}
if(x==-1)
cout<<"NO"<<endl;
else
{
cout<<"YES"<<endl;
string ans="";
for(LL i=0; i<k; i++) ans+="0";
for(LL i=k; i>0&&x; --i)
{
if(x&1) ans[i-1]='1';
x>>=1;
}
cout<<ans<<endl;
}
}
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j.
Input
The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod=1e9+7,p=2,N=2e6;
LL T,n,k;
LL poww[32],pre[N],num[N],vis[N];
char s[N];
LL hashp(LL l,LL r)
{
return (pre[r]-pre[l-1]*poww[r-l+1]%mod+mod)%mod;
}
void init()
{
poww[0]=1;
for(int i=1; i<=27; i++) poww[i]=poww[i-1]<<1;
}
int main()
{
init();
cin>>T;
for(LL ca=1; ca<=T; ca++)
{
cin>>n>>k>>(s+1);
for(LL i=1,ar; i<=n; i++)
{
ar=s[i]-'0';
ar^=1;
pre[i]=(pre[i-1]*p+ar)%mod;
num[i]=num[i-1]+ar;
}
LL limit=poww[min((LL)21,k)]-1;
for(LL i=1; i+k-1<=n; i++)
{
LL x=hashp(max(i,i+k-20),i+k-1);
if(k > 20 && num[i + k - 20] - num[i - 1] > 0) continue;// 超过了 1e6 ,20位以上 有 1
if(x > n) continue; //超过 n 没必要记录
vis[x]=ca;
}
LL x=-1;
for(LL i=0; i<=limit&&x==-1; i++)
{
if(vis[i]!=ca) x=i;
}
if(x==-1)
cout<<"NO"<<endl;
else
{
cout<<"YES"<<endl;
string ans="";
for(LL i=0; i<k; i++) ans+="0";
for(LL i=k; i>0&&x; --i)
{
if(x&1) ans[i-1]='1';
x>>=1;
}
cout<<ans<<endl;
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef pair < int, int > pii;
#define pb push_back
#define khan ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
const int N = 2e6 + 5, mod = 1e9 + 7, INF = 1e9 + 7;
const double eps = 100000;
typedef long long ll;
const ll BIG = 1e18;
const int p1 = 29, p2 = 31;
int n, k, mp1[N];
unsigned long long mp2[N];
string a;
int sub1(int a, int b){
int res = a - b ;
if(res < 0)
res += mod;
return res;
}
int mult1(int a, int b){
return (a * 1ll * b) % mod;
}
struct hsh{
vector < int > v1;
vector < unsigned long long > v2;
void push_back(int val){
v1.pb(((v1.back() * 1ll * p1) % mod + val) % mod);
v2.pb(v2.back() * p2 + val);
}
void pop_back(){
v1.pop_back();
v2.pop_back();
}
pair < int, unsigned long long > get(int l, int r){
//[1; 2]
//[1; 3]
//4 - 1 * 4 = 0
int ans1 = sub1(v1[r + 1], mult1(v1[l], mp1[r - l + 1]));
unsigned long long ans2 = v2[r + 1] - v2[l] * mp2[r - l + 1];
return {ans1, ans2};
}
void generate(string s){
v1.push_back(0);
v2.push_back(0);
for(int i = 0; i < s.size(); i++){
push_back(s[i] - '0');
}
}
};
void solve(){
cin >> n >> k >> a;
for(int i = 0; i < n; i++){
a[i] = ((a[i] - '0') ^ 1) + '0';
}
hsh ahash, shash;
string ans = "";
for(int i = 1; i <= k; i++){
ans += '0';
}
ahash.generate(a);
shash.generate(ans);
set < pair < int, unsigned long long > > hashes;
for(int i = 0; i + k - 1 < n; i++){
hashes.insert(ahash.get(i, i + k - 1));
}
// cout << a << endl;
// for(auto e : ahash.v1){
// cout << e << " ";
// }
// cout << endl;
// for(auto e : hashes){
// cout << e.first << ' ' << e.second << endl;
// }
// cout << endl;
while(true){
if(!hashes.count(shash.get(0, k - 1))){
cout << "YES\n";
cout << ans << endl;
return;
}
while(!ans.empty() and ans.back() == '1'){
shash.pop_back();
ans.pop_back();
}
if(ans.empty()){
cout << "NO\n";
return;
}
ans.pop_back();
shash.pop_back();
ans += '1';
shash.push_back(1);
while(ans.size() < k){
ans += '0';
shash.push_back(0);
}
}
}
void init(){
n = 1e6;
mp1[0] = mp2[0] = 1;
for(int i = 1; i <= n; i++){
mp1[i] = mult1(mp1[i - 1], p1);
mp2[i] = mp2[i - 1] * p2;
// ip1[i] = inv(mp1[i], p1);
// ip2[i] = inv(mp2[i], p2);
}
}
int main(){
khan
init();
int q;
cin >> q;
while(q--){
solve();
}
}
| ### Prompt
In CPP, your task is to solve the following problem:
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j.
Input
The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef pair < int, int > pii;
#define pb push_back
#define khan ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
const int N = 2e6 + 5, mod = 1e9 + 7, INF = 1e9 + 7;
const double eps = 100000;
typedef long long ll;
const ll BIG = 1e18;
const int p1 = 29, p2 = 31;
int n, k, mp1[N];
unsigned long long mp2[N];
string a;
int sub1(int a, int b){
int res = a - b ;
if(res < 0)
res += mod;
return res;
}
int mult1(int a, int b){
return (a * 1ll * b) % mod;
}
struct hsh{
vector < int > v1;
vector < unsigned long long > v2;
void push_back(int val){
v1.pb(((v1.back() * 1ll * p1) % mod + val) % mod);
v2.pb(v2.back() * p2 + val);
}
void pop_back(){
v1.pop_back();
v2.pop_back();
}
pair < int, unsigned long long > get(int l, int r){
//[1; 2]
//[1; 3]
//4 - 1 * 4 = 0
int ans1 = sub1(v1[r + 1], mult1(v1[l], mp1[r - l + 1]));
unsigned long long ans2 = v2[r + 1] - v2[l] * mp2[r - l + 1];
return {ans1, ans2};
}
void generate(string s){
v1.push_back(0);
v2.push_back(0);
for(int i = 0; i < s.size(); i++){
push_back(s[i] - '0');
}
}
};
void solve(){
cin >> n >> k >> a;
for(int i = 0; i < n; i++){
a[i] = ((a[i] - '0') ^ 1) + '0';
}
hsh ahash, shash;
string ans = "";
for(int i = 1; i <= k; i++){
ans += '0';
}
ahash.generate(a);
shash.generate(ans);
set < pair < int, unsigned long long > > hashes;
for(int i = 0; i + k - 1 < n; i++){
hashes.insert(ahash.get(i, i + k - 1));
}
// cout << a << endl;
// for(auto e : ahash.v1){
// cout << e << " ";
// }
// cout << endl;
// for(auto e : hashes){
// cout << e.first << ' ' << e.second << endl;
// }
// cout << endl;
while(true){
if(!hashes.count(shash.get(0, k - 1))){
cout << "YES\n";
cout << ans << endl;
return;
}
while(!ans.empty() and ans.back() == '1'){
shash.pop_back();
ans.pop_back();
}
if(ans.empty()){
cout << "NO\n";
return;
}
ans.pop_back();
shash.pop_back();
ans += '1';
shash.push_back(1);
while(ans.size() < k){
ans += '0';
shash.push_back(0);
}
}
}
void init(){
n = 1e6;
mp1[0] = mp2[0] = 1;
for(int i = 1; i <= n; i++){
mp1[i] = mult1(mp1[i - 1], p1);
mp2[i] = mp2[i - 1] * p2;
// ip1[i] = inv(mp1[i], p1);
// ip2[i] = inv(mp2[i], p2);
}
}
int main(){
khan
init();
int q;
cin >> q;
while(q--){
solve();
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
int n , k;
cin >> n >> k;
string x;
cin >> x;
int pr[n+1];
memset(pr ,0 , sizeof(pr));
for(int i = 0 ; i < n ; i++)
{
if(x[i] == '1')
pr[i+1] = pr[i]+1;
else
pr[i+1] = pr[i];
}
int kk = min((int)log2(n-k+1)+1 , k);
//cout << kk << "\n";
set<int>s;
for(int i = 0; i < (1 << kk) ; i++)
{
s.insert(i);
}
for(int i = k ; i <= n ; i++)
{
int l = i-k;
int r = i-kk;
int cnt = pr[r] - pr[l];
if(cnt == (k-kk))
{
int mask = 0;
for(int j = i-kk+1 ; j <= i ; j++)
{
if(x[j-1] == '0')
{
mask ^= (1 << (i-j));
}
}
// cout << i << " " << mask << "\n";
s.erase(mask);
}
}
if(s.size() == 0)
{
cout << "NO" << "\n";
continue;
}
cout << "YES" << "\n";
string ans;
int rem = *s.begin();
for(int i = 0 ; i < k-kk ; i++)
ans.push_back('0');
string temp ;
for(int i = 0 ; i < kk ; i++)
{
int d = rem%2;
rem /= 2;
temp.push_back(d+48);
}
reverse(temp.begin() , temp.end());
for(int i = 0 ; i < kk ; i++)
ans.push_back(temp[i]);
cout << ans << "\n";
}
} | ### Prompt
Construct a CPP code solution to the problem outlined:
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j.
Input
The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
int n , k;
cin >> n >> k;
string x;
cin >> x;
int pr[n+1];
memset(pr ,0 , sizeof(pr));
for(int i = 0 ; i < n ; i++)
{
if(x[i] == '1')
pr[i+1] = pr[i]+1;
else
pr[i+1] = pr[i];
}
int kk = min((int)log2(n-k+1)+1 , k);
//cout << kk << "\n";
set<int>s;
for(int i = 0; i < (1 << kk) ; i++)
{
s.insert(i);
}
for(int i = k ; i <= n ; i++)
{
int l = i-k;
int r = i-kk;
int cnt = pr[r] - pr[l];
if(cnt == (k-kk))
{
int mask = 0;
for(int j = i-kk+1 ; j <= i ; j++)
{
if(x[j-1] == '0')
{
mask ^= (1 << (i-j));
}
}
// cout << i << " " << mask << "\n";
s.erase(mask);
}
}
if(s.size() == 0)
{
cout << "NO" << "\n";
continue;
}
cout << "YES" << "\n";
string ans;
int rem = *s.begin();
for(int i = 0 ; i < k-kk ; i++)
ans.push_back('0');
string temp ;
for(int i = 0 ; i < kk ; i++)
{
int d = rem%2;
rem /= 2;
temp.push_back(d+48);
}
reverse(temp.begin() , temp.end());
for(int i = 0 ; i < kk ; i++)
ans.push_back(temp[i]);
cout << ans << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i)
#define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i)
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<typename T>void chkmax(T&x,T y){if(x<y)x=y;}
template<typename T>void chkmin(T&x,T y){if(x>y)x=y;}
inline int read(){
#define nc getchar()
int x=0;char c=nc;bool f=0;
while(c<48)f|=c=='-',c=nc;
while(c>47)x=x*10+(c^48),c=nc;
return f?-x:x;
#undef nc
}
typedef double db;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int maxn=1e6+10;
const int P1 = 1e9 + 7, P2 = 1e9 + 9;
int base, pow1[maxn], pow2[maxn];
struct Hash {
int v1, v2;
Hash() {}
Hash(int x, int y) : v1(x), v2(y) {}
ll gvz(){return 1ll*v1*P2+v2;}
void print() {
printf("#(%d %d)\n", v1, v2);
}
void get(Hash &o, char ch) {
v1 = (1ll * o.v1 * base + ch) % P1;
v2 = (1ll * o.v2 * base + ch) % P2;
}
bool operator == (const Hash &o) const {
return v1 == o.v1 && v2 == o.v2;
}
bool operator != (const Hash &o) const {
return v1 != o.v1 || v2 != o.v2;
}
}H[maxn],F[maxn];
Hash getsum(Hash *A, int l, int r) {
return Hash((A[r].v1 + 1ll * (P1 - A[l - 1].v1) * pow1[r - l + 1]) % P1,
(A[r].v2 + 1ll * (P2 - A[l - 1].v2) * pow2[r - l + 1]) % P2);
}
void init() {
base = Rnd();
if (base < 0) base = -base;
base %= 19260817, base += 257;
pow1[0] = pow2[0] = 1;
rep(i, 1, maxn - 1) {
pow1[i] = 1ll * base * pow1[i - 1] % P1;
pow2[i] = 1ll * base * pow2[i - 1] % P2;
}
rep(i,1,maxn-1){
F[i].get(F[i>>1],i&1);
}
}
int n,k,A[maxn];
char str[maxn];
void solve(){
scanf("%d%d%s",&n,&k,str+1);
rep(i,1,n)A[i]=(str[i]-'0')^1,H[i].get(H[i-1],A[i]);
set<ll>vis;
rep(i,1,n-k+1)vis.insert(getsum(H,i,i+k-1).gvz());
rep(i,0,min(n-k+2,k<=25?(1<<k)-1:n)){
if(!vis.count(F[i].gvz())){
puts("YES");
static int ans[maxn];
memset(ans,0,(k+1)<<2);
int x=i,ps=k;
while(x)ans[ps--]=x&1,x>>=1;
rep(i,1,k)printf("%d",ans[i]);
puts("");
return;
}
}
puts("NO");
}
signed main(){
init();
int T=read();
while(T--)solve();
// solve();
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j.
Input
The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i)
#define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i)
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<typename T>void chkmax(T&x,T y){if(x<y)x=y;}
template<typename T>void chkmin(T&x,T y){if(x>y)x=y;}
inline int read(){
#define nc getchar()
int x=0;char c=nc;bool f=0;
while(c<48)f|=c=='-',c=nc;
while(c>47)x=x*10+(c^48),c=nc;
return f?-x:x;
#undef nc
}
typedef double db;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int maxn=1e6+10;
const int P1 = 1e9 + 7, P2 = 1e9 + 9;
int base, pow1[maxn], pow2[maxn];
struct Hash {
int v1, v2;
Hash() {}
Hash(int x, int y) : v1(x), v2(y) {}
ll gvz(){return 1ll*v1*P2+v2;}
void print() {
printf("#(%d %d)\n", v1, v2);
}
void get(Hash &o, char ch) {
v1 = (1ll * o.v1 * base + ch) % P1;
v2 = (1ll * o.v2 * base + ch) % P2;
}
bool operator == (const Hash &o) const {
return v1 == o.v1 && v2 == o.v2;
}
bool operator != (const Hash &o) const {
return v1 != o.v1 || v2 != o.v2;
}
}H[maxn],F[maxn];
Hash getsum(Hash *A, int l, int r) {
return Hash((A[r].v1 + 1ll * (P1 - A[l - 1].v1) * pow1[r - l + 1]) % P1,
(A[r].v2 + 1ll * (P2 - A[l - 1].v2) * pow2[r - l + 1]) % P2);
}
void init() {
base = Rnd();
if (base < 0) base = -base;
base %= 19260817, base += 257;
pow1[0] = pow2[0] = 1;
rep(i, 1, maxn - 1) {
pow1[i] = 1ll * base * pow1[i - 1] % P1;
pow2[i] = 1ll * base * pow2[i - 1] % P2;
}
rep(i,1,maxn-1){
F[i].get(F[i>>1],i&1);
}
}
int n,k,A[maxn];
char str[maxn];
void solve(){
scanf("%d%d%s",&n,&k,str+1);
rep(i,1,n)A[i]=(str[i]-'0')^1,H[i].get(H[i-1],A[i]);
set<ll>vis;
rep(i,1,n-k+1)vis.insert(getsum(H,i,i+k-1).gvz());
rep(i,0,min(n-k+2,k<=25?(1<<k)-1:n)){
if(!vis.count(F[i].gvz())){
puts("YES");
static int ans[maxn];
memset(ans,0,(k+1)<<2);
int x=i,ps=k;
while(x)ans[ps--]=x&1,x>>=1;
rep(i,1,k)printf("%d",ans[i]);
puts("");
return;
}
}
puts("NO");
}
signed main(){
init();
int T=read();
while(T--)solve();
// solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define FOR(a,b) for (int a=0; a<b; a++)
#define FUR(a,b) for (int a=1; a<=b; a++)
#define ROF(a,b) for (int a=b; a-->0; )
#define RUF(a,b) for (int a=b; a>0; a--)
#define FORR(a,b,c) for (int a=b; a<c; a++)
#define all(v) begin(v),end(v)
#define rall(v) rbegin(v),rend(v)
#define xx first
#define yy second
#define ass assign
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define ll long long
#define V vector
#define pii pair<int,int>
template<class T> ostream& operator<<(ostream& os, const vector<T>& v) {
for (const T& x: v) os << x << ' ';
return os; }
template<class T> istream& operator>>(istream& is, vector<T>& v) {
for (T& x: v) is >> x;
return is; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef LOCAL // compile with -DLOCAL
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
template<class T> bool miin(T& a, const T& b) { return a > b ? a=b,1 : 0; }
template<class T> bool maax(T& a, const T& b) { return a < b ? a=b,1 : 0; }
const int MOD = 1e9+7; // 998244353;
const ll INF = 1e18;
const char nl = '\n';
const double PI = acos(-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int q; cin >> q;
FOR(test,q) {
int n,k; cin >> n >> k;
string s; cin >> s;
V<int> c0(n+1);
FOR(i,n) c0[i+1] = c0[i] + (s[i]=='0');
int poss = n-k+1;
int pre0 = k - ceil(log2(poss + 1));
pre0 = max(pre0, 0);
int post = k-pre0;
cerr << poss << ' ' << pre0 << ' ' << post << '+';
unordered_set<string> seen;
FOR(i,poss) {
if (c0[i] < c0[i+pre0]) continue; // already ok
string x = s.substr(i+pre0, post);
seen.insert(x);
}
int ans;
string kek(post,'0');
int MAX = 1 << post;
for (ans=0; ans<MAX; ans++) {
FOR(i,post) kek[post-i-1] =
( ans & (1<<i) ? '0' : '1' );
if (!seen.count(kek)) {
for (char&c: kek) c = ( c=='0' ? '1' : '0' );
break;
}
}
if (ans == MAX) cout << "NO\n";
else cout << "YES\n" << string(pre0,'0') << kek << nl;
}
}
| ### Prompt
Create a solution in Cpp for the following problem:
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j.
Input
The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define FOR(a,b) for (int a=0; a<b; a++)
#define FUR(a,b) for (int a=1; a<=b; a++)
#define ROF(a,b) for (int a=b; a-->0; )
#define RUF(a,b) for (int a=b; a>0; a--)
#define FORR(a,b,c) for (int a=b; a<c; a++)
#define all(v) begin(v),end(v)
#define rall(v) rbegin(v),rend(v)
#define xx first
#define yy second
#define ass assign
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define ll long long
#define V vector
#define pii pair<int,int>
template<class T> ostream& operator<<(ostream& os, const vector<T>& v) {
for (const T& x: v) os << x << ' ';
return os; }
template<class T> istream& operator>>(istream& is, vector<T>& v) {
for (T& x: v) is >> x;
return is; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef LOCAL // compile with -DLOCAL
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
template<class T> bool miin(T& a, const T& b) { return a > b ? a=b,1 : 0; }
template<class T> bool maax(T& a, const T& b) { return a < b ? a=b,1 : 0; }
const int MOD = 1e9+7; // 998244353;
const ll INF = 1e18;
const char nl = '\n';
const double PI = acos(-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int q; cin >> q;
FOR(test,q) {
int n,k; cin >> n >> k;
string s; cin >> s;
V<int> c0(n+1);
FOR(i,n) c0[i+1] = c0[i] + (s[i]=='0');
int poss = n-k+1;
int pre0 = k - ceil(log2(poss + 1));
pre0 = max(pre0, 0);
int post = k-pre0;
cerr << poss << ' ' << pre0 << ' ' << post << '+';
unordered_set<string> seen;
FOR(i,poss) {
if (c0[i] < c0[i+pre0]) continue; // already ok
string x = s.substr(i+pre0, post);
seen.insert(x);
}
int ans;
string kek(post,'0');
int MAX = 1 << post;
for (ans=0; ans<MAX; ans++) {
FOR(i,post) kek[post-i-1] =
( ans & (1<<i) ? '0' : '1' );
if (!seen.count(kek)) {
for (char&c: kek) c = ( c=='0' ? '1' : '0' );
break;
}
}
if (ans == MAX) cout << "NO\n";
else cout << "YES\n" << string(pre0,'0') << kek << nl;
}
}
``` |
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
char str[1001000];
pair<bool, int> check[(1<<23)];
int suf[1001000];
int dp[1001000];
int main(){
int t;
scanf("%d", &t);
while(t--){
int n, k;
scanf("%d %d", &n, &k);
scanf("%s", str);
suf[0]=str[0]-'0';
dp[0]=suf[0];
if (k==1) check[dp[0]]=make_pair(1, t);
for (int i=1;i<n;i++){
if (str[i]=='1') suf[i]=suf[i-1]+1;
else suf[i]=0;
int tmp;
if (k>22)tmp=dp[i-1]&(~(1<<21));
else tmp=dp[i-1]&(~(1<<(k-1)));
tmp <<=1;
tmp+=(str[i]-'0');
dp[i]=tmp;
//printf("dp %d: %d\n", i, tmp);
if (i<k-1) continue;
if (k>21 && i-22>=0){
if (suf[i-22]>=k-22){
check[dp[i]].first=1;
check[dp[i]].second=t;
}
}
else{
check[dp[i]].first=1;
check[dp[i]].second=t;
}
}
int xx=min(22, k);
int anss;
bool test=1;
for (int i=(1<<xx)-1;i>=0;i--){
if (!check[i].first || check[i].second!=t){
test=0;
anss=i;
break;
}
}
if (test) printf("NO\n");
else{
printf("YES\n");
int yy=max(0, k-22);
for (int i=0;i<yy;i++) printf("0");
for (int i=xx-1;i>=0;i--){
if (anss & (1<<i)) printf("0");
else printf("1");
}
printf("\n");
}
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j.
Input
The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
### Response
```cpp
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
char str[1001000];
pair<bool, int> check[(1<<23)];
int suf[1001000];
int dp[1001000];
int main(){
int t;
scanf("%d", &t);
while(t--){
int n, k;
scanf("%d %d", &n, &k);
scanf("%s", str);
suf[0]=str[0]-'0';
dp[0]=suf[0];
if (k==1) check[dp[0]]=make_pair(1, t);
for (int i=1;i<n;i++){
if (str[i]=='1') suf[i]=suf[i-1]+1;
else suf[i]=0;
int tmp;
if (k>22)tmp=dp[i-1]&(~(1<<21));
else tmp=dp[i-1]&(~(1<<(k-1)));
tmp <<=1;
tmp+=(str[i]-'0');
dp[i]=tmp;
//printf("dp %d: %d\n", i, tmp);
if (i<k-1) continue;
if (k>21 && i-22>=0){
if (suf[i-22]>=k-22){
check[dp[i]].first=1;
check[dp[i]].second=t;
}
}
else{
check[dp[i]].first=1;
check[dp[i]].second=t;
}
}
int xx=min(22, k);
int anss;
bool test=1;
for (int i=(1<<xx)-1;i>=0;i--){
if (!check[i].first || check[i].second!=t){
test=0;
anss=i;
break;
}
}
if (test) printf("NO\n");
else{
printf("YES\n");
int yy=max(0, k-22);
for (int i=0;i<yy;i++) printf("0");
for (int i=xx-1;i>=0;i--){
if (anss & (1<<i)) printf("0");
else printf("1");
}
printf("\n");
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e6 + 10;
char s[N];
int a[N];
char t[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
cin >> s;
for (int i = 0; i < n; ++i) {
a[i] = (i == 0 ? 0 : a[i - 1]) + (s[i] == '0');
}
set<int> da;
for (int i = 0; i <= n - k; ++i) {
int val = 0;
if (k > 20 && a[i + k - 21] - (i == 0 ? 0 : a[i - 1]) > 0)
continue;
for (int j = max(i, i + k - 20), bs = 1; j < i + k; ++j) {
val = val * 2 + (s[j] == '0');
}
da.emplace(val);
}
for (int i = 0; ; ++i) {
if (k <= 20 && (1 << k) <= i) {
cout << "NO" << endl;
break;
}
if (da.find(i) != da.end())
continue;
int l = 0;
for (int j = i; j; j /= 2) {
t[l++] = j % 2 + '0';
}
for (; l < k && l < 20; ) {
t[l++] = '0';
}
reverse(t, t + l);
cout << "YES" << endl;
for (int j = 0; j < k - l; ++j)
cout << '0';
for (int j = 0; j < l; ++j)
cout << t[j];
cout << endl;
break;
}
}
} | ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j.
Input
The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e6 + 10;
char s[N];
int a[N];
char t[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
cin >> s;
for (int i = 0; i < n; ++i) {
a[i] = (i == 0 ? 0 : a[i - 1]) + (s[i] == '0');
}
set<int> da;
for (int i = 0; i <= n - k; ++i) {
int val = 0;
if (k > 20 && a[i + k - 21] - (i == 0 ? 0 : a[i - 1]) > 0)
continue;
for (int j = max(i, i + k - 20), bs = 1; j < i + k; ++j) {
val = val * 2 + (s[j] == '0');
}
da.emplace(val);
}
for (int i = 0; ; ++i) {
if (k <= 20 && (1 << k) <= i) {
cout << "NO" << endl;
break;
}
if (da.find(i) != da.end())
continue;
int l = 0;
for (int j = i; j; j /= 2) {
t[l++] = j % 2 + '0';
}
for (; l < k && l < 20; ) {
t[l++] = '0';
}
reverse(t, t + l);
cout << "YES" << endl;
for (int j = 0; j < k - l; ++j)
cout << '0';
for (int j = 0; j < l; ++j)
cout << t[j];
cout << endl;
break;
}
}
}
``` |
/*
_|_| _| _| _|
_| _| _| _|_| _|_|_|_| _| _| _|
_| _| _|_| _| _| _|_|
_| _| _| _| _| _| _| _|
_|_| _| _|_|_|_| _|_| _| _|
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<unordered_map>
//#define ls (rt<<1)
//#define rs (rt<<1|1)
#define vi vector<int>
#define pb push_back
#define mk make_pair
#define pii pair<int,int>
#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;i++)
#define fi first
#define se second
typedef long long ll;
using namespace std;
const int maxn=1e6+100;
char s[maxn],trans[maxn];
int n,k;
struct Hash{
int pow[maxn],mod,base,pre[maxn];
unordered_map<int,bool>ck;
int tar[maxn];
int ksm(int num,int t){
int res=1;
for(;t;t>>=1,num=1ll*num*num%mod){
if(t&1)res=1ll*num*res%mod;
}
return res;
}
void build(){
pow[0]=1;
rep(i,1,maxn-1)pow[i]=1ll*pow[i-1]*base%mod;
}
void init(){
ck.clear();
rep(i,1,n)pre[i]=1ll*pow[i]*s[i]%mod;
rep(i,1,n)pre[i]=(pre[i]+pre[i-1])%mod;
rep(i,1,k)tar[i]=1ll*pow[i]*trans[i]%mod;
rep(i,1,k)tar[i]=(tar[i]+tar[i-1])%mod;
}
int seg(int l,int r){
int res=1ll*(pre[r]-pre[l-1])*ksm(pow[l-1],mod-2)%mod;
res=(res%mod+mod)%mod;
return res;
}
void recalc(int st){
rep(i,st,k){
tar[i]=1ll*pow[i]*trans[i]%mod;
tar[i]=(tar[i]+tar[i-1])%mod;
}
}
}h[2];
void solve(){
scanf("%d%d",&n,&k);rep(i,1,k)trans[i]='1';
scanf("%s",s+1);h[0].init();h[1].init();
for(int l=1,r=k;r<=n;l++,r++){
h[0].ck[h[0].seg(l,r)]=1;
h[1].ck[h[1].seg(l,r)]=1;
}
if(!h[0].ck[h[0].tar[k]]||!h[1].ck[h[1].tar[k]]){
printf("YES\n");
rep(i,1,k)printf("%d",(trans[i]-'0')^1);printf("\n");return;
}
ll lim=0;
if(k>23)lim=(1<<23);
else lim=(1<<k)-2;
for(ll i=lim;i>=0;i--){
int flag=-1;
for(int j=k;j>=1;j--){
if(trans[j]=='1'){trans[j]='0';flag=j;break;}
else {trans[j]='1';}
}
h[0].recalc(flag);h[1].recalc(flag);
if(!h[0].ck[h[0].tar[k]]||!h[1].ck[h[1].tar[k]]){
printf("YES\n");
rep(i,1,k)printf("%d",(trans[i]-'0')^1);printf("\n");return;
}
}
printf("NO\n");
}
int main(){
h[0].mod=998244353;h[0].base=233;h[0].build();
h[1].mod=1e9+7;h[1].base=2333;h[1].build();
int t;scanf("%d",&t);
while(t--)solve();
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i.
You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j).
Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n].
Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j.
Input
The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines.
The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, print the answer as follows:
* if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example);
* otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1.
Example
Input
7
4 2
0110
4 2
1001
9 3
010001110
9 3
101110001
10 3
0101110001
10 10
1111111111
11 10
11111111110
Output
YES
11
YES
00
YES
010
YES
101
NO
YES
0000000001
YES
0000000010
### Response
```cpp
/*
_|_| _| _| _|
_| _| _| _|_| _|_|_|_| _| _| _|
_| _| _|_| _| _| _|_|
_| _| _| _| _| _| _| _|
_|_| _| _|_|_|_| _|_| _| _|
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<unordered_map>
//#define ls (rt<<1)
//#define rs (rt<<1|1)
#define vi vector<int>
#define pb push_back
#define mk make_pair
#define pii pair<int,int>
#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;i++)
#define fi first
#define se second
typedef long long ll;
using namespace std;
const int maxn=1e6+100;
char s[maxn],trans[maxn];
int n,k;
struct Hash{
int pow[maxn],mod,base,pre[maxn];
unordered_map<int,bool>ck;
int tar[maxn];
int ksm(int num,int t){
int res=1;
for(;t;t>>=1,num=1ll*num*num%mod){
if(t&1)res=1ll*num*res%mod;
}
return res;
}
void build(){
pow[0]=1;
rep(i,1,maxn-1)pow[i]=1ll*pow[i-1]*base%mod;
}
void init(){
ck.clear();
rep(i,1,n)pre[i]=1ll*pow[i]*s[i]%mod;
rep(i,1,n)pre[i]=(pre[i]+pre[i-1])%mod;
rep(i,1,k)tar[i]=1ll*pow[i]*trans[i]%mod;
rep(i,1,k)tar[i]=(tar[i]+tar[i-1])%mod;
}
int seg(int l,int r){
int res=1ll*(pre[r]-pre[l-1])*ksm(pow[l-1],mod-2)%mod;
res=(res%mod+mod)%mod;
return res;
}
void recalc(int st){
rep(i,st,k){
tar[i]=1ll*pow[i]*trans[i]%mod;
tar[i]=(tar[i]+tar[i-1])%mod;
}
}
}h[2];
void solve(){
scanf("%d%d",&n,&k);rep(i,1,k)trans[i]='1';
scanf("%s",s+1);h[0].init();h[1].init();
for(int l=1,r=k;r<=n;l++,r++){
h[0].ck[h[0].seg(l,r)]=1;
h[1].ck[h[1].seg(l,r)]=1;
}
if(!h[0].ck[h[0].tar[k]]||!h[1].ck[h[1].tar[k]]){
printf("YES\n");
rep(i,1,k)printf("%d",(trans[i]-'0')^1);printf("\n");return;
}
ll lim=0;
if(k>23)lim=(1<<23);
else lim=(1<<k)-2;
for(ll i=lim;i>=0;i--){
int flag=-1;
for(int j=k;j>=1;j--){
if(trans[j]=='1'){trans[j]='0';flag=j;break;}
else {trans[j]='1';}
}
h[0].recalc(flag);h[1].recalc(flag);
if(!h[0].ck[h[0].tar[k]]||!h[1].ck[h[1].tar[k]]){
printf("YES\n");
rep(i,1,k)printf("%d",(trans[i]-'0')^1);printf("\n");return;
}
}
printf("NO\n");
}
int main(){
h[0].mod=998244353;h[0].base=233;h[0].build();
h[1].mod=1e9+7;h[1].base=2333;h[1].build();
int t;scanf("%d",&t);
while(t--)solve();
}
``` |