AT_arc227_d Median of Binary Strings 题解

· · 题解

:::::info[闲话] atc 求求你那帮出题人别出猜猜乐了。 ::::: :::::success[Hint 1] 观察数据范围猜测时间复杂度,考察必要条件。 :::::

对应 Hint 1:

我们观察数据范围猜测时间复杂度,我们猜测这个题是三次方的,我们考虑考察必要条件。

我们容易发现若某个 t_i 的某一位是 \texttt1 而所有 s_i 的这一位都是 \texttt0,那么这个 t_i 就是无解的,但是这个必要条件看起来不太充要,我们考虑继续观察。

我们考虑到时间复杂度是三次方的,我们不妨考察一下二元的关系,我们考察一下某个 t_i 的两个下标 i_1,i_2,我们猜测必须要存在 s_i 使得 i_1,i_2 两个下标上的字符都和 t_i 相同,我们考虑证明这一点。

我们不失一般性地假设两个下标为一开始的两个下标,不失一般性地假设为两个 \texttt0,那么要使得第一位变成 \texttt0 我们必须在合并时选择两个 \texttt{01} 开头的串,那么第二位一定会变成 \texttt1,这样我们就证明完毕这是必要的了。

我们大胆猜测这是充要的,考虑证明。

我们考虑将其扩展到三个下标,我们考虑到若我们需要合成由 abc 开头的字符串,根据上面的要求我们可以得到一个 abx 开头的、一个 ayc 开头的和一个 zbc 开头的,对这三个进行一次操作即可。

对于更多下标也是类似的递推方式。

这样我们就做完了这道题。
时间复杂度为 \Theta((n+q)m^2),空间复杂度为 \Theta(m^2)。

:::::success[code]

#include<bits/stdc++.h>
#define inl inline
#define int long long
#define lll __int128
#define fst ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,x,y) for(int i=x;i<=(y);++i) 
#define per(i,x,y) for(int i=x;i>=(y);--i)
#define rpr(i,x,y,z) for(int i=x;i<=(y);i+=z)
#define epe(i,x,y,z) for(int i=x;i>=(y);i-=z)
#define repe(i,x,y) for(i=x;i<=(y);++i) 
#define endl '\n'
#define INF 1e18
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front 
#define fi first
#define se second
#define lcm(x,y) x/__gcd(x,y)*y
#define ull unsigned long long
#define prr make_pair
#define pii pair<int,int> 
#define gt(s) getline(cin,s)
#define at(x,y) for(const auto &x:y)
#define ff fflush(stdout)
#define mt(x,y) memset(x,y,sizeof(x))
#define idg isdigit
#define fp(s) string ssss=s;freopen((ssss+".in").c_str(),"r",stdin);freopen((ssss+\
".out").c_str(),"w",stdout);
#define sstr stringstream 
#define all(x) x.begin(),x.end()
#define mcy(a,b) memcpy(a,b,sizeof(b))
#define ui unsigned
#define si signed
#define eb emplace_back
#define pff(x) ((x)*(x))
#define eush emplace
#define double long double
#define pdi pair<double,int>
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
#ifdef __unix__
#define pc putchar_unlocked
#else
#define pc _putchar_nolock
#endif
using namespace std;
const int N=505;
bool vis[N][N][2][2];
si main(){
    fst;
    int n,m,q;
    cin>>n>>m>>q;
    rep(i,1,n){
        string s;
        cin>>s;
        s=' '+s;
        rep(j,1,m)
            rep(k,1,m) vis[j][k][s[j]^'0'][s[k]^'0']=1;
    }
    rep(i,1,q){
        string s;
        cin>>s;
        s=' '+s; 
        bool flag=1;
        rep(j,1,m)
            rep(k,1,m) flag&=vis[j][k][s[j]^'0'][s[k]^'0'];
        if(flag) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
} 

:::::