Problem Statement
You are given a vector t that describes a rectangular table of zeroes and ones. Each character in t is either ‘0’ or ‘1’.
We say that a table is nice if there are two sequences x, y of zeroes and ones such that for each valid pair of indices i, j we have t[i][j] = x[i] xor y[j].
Some technical remarks:
The number of elements in x should be equal to the number of rows of the table, i.e., the number of elements in t.
The number of elements in y should be equal to the number of columns of the table, i.e., the length of each string in t.
The operation xor (exclusive or) is defined as follows: 0 xor 0 = 0, 1 xor 1 = 0, 0 xor 1 = 1, and 1 xor 0 = 1.
Verify whether the given table is nice. Return “Nice” if it is nice and “Not nice” otherwise. Note that the return value is case-sensitive.
Definition
Class:
NiceTable
Method:
isNice
Parameters:
vector
Returns:
string
Method signature:
string isNice(vector t)
(be sure your method is public)
Limits
Time limit (s):
2.000
Memory limit (MB):
512
Stack limit (MB):
512
Constraints
t will contain between 1 and 5 elements, inclusive.
Each element of t will contain between 1 and 5 characters, inclusive.
All elements of t will contain the same number of characters.
Each element of t will consist only of characters ‘0’ and ‘1’.
Examples
0)
{“01”,
“10”}
Returns: “Nice”
One valid choice is to choose x = y = {1, 0}.
1)
{“01”,
“11”}
Returns: “Not nice”
Assume that t is nice. The sequences x and y have to satisfy the following constraints:
x[0] xor y[0] = 0
x[0] xor y[1] = 1
x[1] xor y[0] = 1
x[1] xor y[1] = 1
From the first constraint we see that x[0] = y[0]. From the second and the third constraint we can then derive that we must also have x[1] = y[1]. But then the fourth constraint is not satisfied, which is a contradiction. Therefore, this t is not nice.
2)
{“0100”,
“1011”,
“0100”}
Returns: “Nice”
Here, one valid choice is x = {1, 0, 1} and y = {1, 0, 1, 1}.
3)
{“11”,
“10”,
“11”,
“11”,
“11”}
Returns: “Not nice”
【题目链接】:
【题意】
给你一个n*m的矩阵a;
然后问你是否存在一个长度为n的序列x和长度为m的序列y;
使得s[i]^s[j]=a[i][j]对于所有的i,j皆成立;
n,m<=5
a[i][j]中只会出现0和1
【题解】
暴力枚举x,y的每一位是0还是1就好;
【Number Of WA】
0
【反思】
一开始想错了,以为让x全都是0就好;
后来想,或许可以让x的第一位是0,然后其他位先不定;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;
//head
int x[8],y[8],hang,lie,ok;
vector <string> a;
void dfs2(int now){
if (now > lie-1){
int temp = 1;
rep1(i,0,hang-1){
rep1(j,0,lie-1){
int k = a[i][j]-'0';
if (k!=(x[i]^y[j]))
temp = 0;
}
}
if (temp) ok = 1;
return;
}
y[now] = 0;
dfs2(now+1);
y[now] = 1;
dfs2(now+1);
}
void dfs1(int now){
if (now > hang-1){
dfs2(0);
return;
}
x[now] = 0;
dfs1(now+1);
x[now] = 1;
dfs1(now+1);
}
class NiceTable
{
public:
string isNice(vector <string> t)
{
a = t;
hang = t.size(),lie = t[0].size();
ok = 0;
dfs1(0);
if (ok)
return "Nice";
else
return "Not nice";
}
};