[ARC227D] Median of Binary Strings

· · 题解

场上完全不会这题。。。感觉这题很难啊。。。

思路:

考虑找性质,对于 T_1, T_2, T_3,变换出来的字符串 S,考虑它的任意两位 x, y,显然 T_i 中有至少两个的第 x 位和 S_x 相同,然后考虑这两个串的第 y 位,显然它俩至少存在一个和 S_y 相同,因为如果都不同的话,变换后应该是 S_y \oplus 1。

于是得出结论,对于一个变换出来的字符串 S,必然满足前面对于其的任意两位 x, y,T_i 中都存在一个 x, y 位和 S 相同的字符串;如果 T_i 不是初始字符集,那就继续往前推,选择它的两位找上一个 T_i' 使得 x, y 位相同,总能找到初始字符串集的一个。

于是,只需要在初始的 n 个 S_i 中存在 x, y 位都相同的即可,显然这是必要的;实际上这也是充要的;容易数学归纳法证明:

于是得证;直接扫一遍 x, y check 即可,时间复杂度位 O((n + q)m^2)。

完整代码:

#include<bits/stdc++.h>
#define fi first
#define se second
#define lowbit(x) x & (-x)
using namespace std;
typedef long long ll;
const int N = 505;
inline ll read(){
    ll x = 0, f = 1;
    char c = getchar();
    while(c < '0' || c > '9'){
        if(c == '-')
          f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9'){
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return x * f;
} 
inline void write(ll x){
    if(x < 0){
        putchar('-');
        x = -x;
    }
    if(x > 9)
      write(x / 10);
    putchar(x % 10 + '0');
}
int n, m, q;
char s[N];
bool chk[N][N][2][2];
int main(){
    n = read(), m = read(), q = read();
    for(int i = 1; i <= n; ++i){
        scanf("%s", s + 1);
        for(int l = 1; l <= m; ++l)
          for(int r = l; r <= m; ++r)
            chk[l][r][s[l] - '0'][s[r] - '0'] = 1;
    }
    while(q--){
        scanf("%s", s + 1);
        bool flag = 1;
        for(int l = 1; l <= m; ++l)
          for(int r = l; r <= m; ++r)
            flag &= chk[l][r][s[l] - '0'][s[r] - '0'];
        if(!flag){
            puts("No");
            continue;
        }
        puts("Yes");
    }
    return 0;
}