如题
/ 题目: CF1792D Fixed Prefix Permutations
// 链接: https://www.luogu.com.cn/problem/CF1792D
// 难度: 普及+/提高
// 题解:
// - 洛谷: https://www.luogu.com.cn/problem/solution/CF1792D
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int nxt[maxn][15], idx = 1, n, m, a[maxn][15], pos[15];
void init() {
for (int i = 1; i <= idx; ++i) {
for (int j = 0; j < 10; ++j) {
nxt[i][j] = 0;
}
}
idx = 1;
}
void add(int c[]) {
int p = 1;
for (int i = 1; i <= n; ++i) {
int x = c[i];
if (!nxt[p][x]) {
nxt[p][x] = ++idx;
}
p = nxt[p][x];
}
}
void best_coder() {
int t;
cin >> t;
while (t--) {
cin >> m >> n;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> a[i][j];
}
}
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
pos[j] = 0;
}
for (int j = 1; j <= n; ++j) {
pos[a[i][j]] = j;
}
add(pos);
}
for (int i = 1; i <= m; ++i) {
for (int j = 1, u = 1; j <= n; ++j) {
if (!nxt[u][a[i][j]]) {
cout << j - 1 << " ";
break;
} else {
u = nxt[u][a[i][j]];
}
if (j == n) {
cout << n << " ";
}
}
}
init();
cout << '\n';
}
}
void happy_coder() {
}
int main() {
// 提升cin、cout效率
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// 小码匠
best_coder();
// 最优解
// happy_coder();
return 0;
}