场上完全不会这题。。。感觉这题很难啊。。。
思路:
考虑找性质,对于 ,变换出来的字符串 ,考虑它的任意两位 ,显然 中有至少两个的第 位和 相同,然后考虑这两个串的第 位,显然它俩至少存在一个和 相同,因为如果都不同的话,变换后应该是 。
于是得出结论,对于一个变换出来的字符串 ,必然满足前面对于其的任意两位 , 中都存在一个 位和 相同的字符串;如果 不是初始字符集,那就继续往前推,选择它的两位找上一个 使得 位相同,总能找到初始字符串集的一个。
于是,只需要在初始的 个 中存在 位都相同的即可,显然这是必要的;实际上这也是充要的;容易数学归纳法证明:
-
设我们可以复原还原出 个固定位置。
-
当 时,设要还原 ,那么由于上面结论,存在 这三个串,操作后就是 (这里 通配符,即任意字符都行)。
-
对于 ,设要还原 ,考虑先还原 ,然后再操作这三个就可以得到答案。
于是得证;直接扫一遍 check 即可,时间复杂度位 。
完整代码:
#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;
}