题意
你有一个长度为 n ( 2 ≤ n ≤ 2 ∗ 1 0 4 ) n(2 \le n \le 2*10^4) n(2≤n≤2∗104)的二进制 01 串 01串 01串
你需要找到不相交的两个长度至少为 ⌊ n 2 ⌋ ⌊\frac{n}{2}⌋ ⌊2n⌋的二进制串使得它们互为倍数关系(也可以相等)
思路
若能在 [ n / 2 + 1 , n ] [n/2+1,n] [n/2+1,n]内找到一个 0 0 0,那答案即为 [ 1 , p o s ] , [ 1 , p o s − 1 ] [1,pos],[1,pos-1] [1,pos],[1,pos−1]( 2 2 2倍), p o s pos pos为 0 0 0所在的位置
否则在 [ n / 2 + 1 , n ] [n/2+1,n] [n/2+1,n]内都为 1 1 1
- 若 p o s = n / 2 pos=n/2 pos=n/2的值为 0 0 0,则 [ n / 2 , n ] , [ n / 2 + 1 , n ] [n/2,n],[n/2+1,n] [n/2,n],[n/2+1,n]表示的二进制串相等
- 若 p o s = n / 2 pos=n/2 pos=n/2的值为 1 1 1,则 [ n / 2 , n − 1 ] , [ n / 2 + 1 , n ] [n/2,n-1],[n/2+1,n] [n/2,n−1],[n/2+1,n]表示的二进制串相等
综上模拟即可
Code
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
#pragma GCC optimize(3)
typedef long long ll;
#define INF 0x3f3f3f3f
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
#define iss ios::sync_with_stdio(false)
#define debug(x) cout << #x << ": " << x << endl;
inline ll read() {
ll s = 0, w = 1;
char ch = getchar();
while (ch < 48 || ch > 57) {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= 48 && ch <= 57)
s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
return s * w;
}
char s[20005];
int main() {
int t = read();
while (t--) {
int n = read();
scanf("%s", s + 1);
int pos = -1;
for (int i = n / 2 + 1; i <= n; ++i) {
if (s[i] == '0') {
pos = i;
break;
}
}
if (pos != -1) {
cout << 1 << " " << pos << " ";
cout << 1 << " " << pos - 1 << endl;
} else {
if (s[n / 2] == '0') {
cout << n / 2 << " " << n << " ";
cout << n / 2 + 1 << " " << n << endl;
} else {
cout << n / 2 << " " << n - 1 << " ";
cout << n / 2 + 1 << " " << n << endl;
}
}
}
}