题目:传送门
题意:
定义一个36位进制的概念,‘0’-‘9’ ‘A’-‘Z’,输入一个n,保证接下来输入的数字是n进制,接下来输入一个二维矩阵。满足一些条件:
如果二维矩阵中的每一行每一列都是全排列,则继续。否则输出“NO”
如果二维矩阵中的第一行和第一列是递增的,则输出“Reduced”,否则输出“Not Reduced”
题解:
题目比较容易懂,纯模拟。
有一个简单的方法判断全排列,就是只需要判断第一行是不是全排列,然后sort每一行每一列,判断是不是与第一行相等就行了。(所以我们要用string,因为char是无法sort的),判断是否升序也很简单,sort一遍,然后与原始字符串比较就行了,相等则升序,否则不满足。
有一个比较坑的点,就是要多组输入,如果不多组,会WA4
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#define debug(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
using namespace std;
int main(void) {
int n;
while (cin >> n) {
bool is_latin = true, is_reduce = true;
vector<string> ori(n);
for (int i = 0; i < n; i++)
cin >> ori[i];
string top_row = ori[0];
sort(top_row.begin(), top_row.end());
for (int i = 1; i < (int)top_row.size(); i++)
if (top_row[i] == top_row[i - 1]) //判断第一行是否全排列
is_latin = false;
if (ori[0] != top_row) //检查第一行是否递增
is_reduce = false;
for (int i = 0; i < n; i++) {
string row_and_column = ori[i];
sort(row_and_column.begin(), row_and_column.end()); //string 能够被 sort 以字典序的方式
if (row_and_column != top_row)//检查每一行是否全排列
is_latin = false;
for (int j = 0; j < n; j++)
row_and_column[j] = ori[j][i];
if (i == 0 && row_and_column != top_row)//检查第一列是否递增
is_reduce = false;
sort(row_and_column.begin(), row_and_column.end());
if (row_and_column != top_row)//检查每一列是否全排列
is_latin = false;
}
if (is_latin == false)
puts("No");
else if (is_reduce == false)
puts("Not Reduced");
else
puts("Reduced");
}
return 0;
}