#include <bits/stdc++.h>
using namespace std;
#define int long long
const int Z = 35;
int n, m, k, dp[Z][Z];
string a[Z];
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0);
for(int i = 1; i < Z; ++i)
for(int j = 1; j < Z; ++j)
dp[i][j] = dp[i-1][j] + dp[i][j-1], dp[1][1] = 1;
cin >> n >> m;
for(int i = 0; i < n; ++i)
cin >> a[i];
cin >> k;
string ans(n+m-1, a[0][0]);
map<array<int, 2>, int> cur, nxt;
cur[{0, 0}] = 1;
for(int i = 0; i < n+m-1; ++i) {
map<char, int> sum;
for(auto &[j, cnt] : cur) {
auto [x, y] = j;
sum[a[x][y]] += dp[n-x][m-y] * cnt;
}
for(auto &[c, s] : sum) {
ans[i] = c;
if(s < k) k -= s;
else break;
}
for(auto &[j, cnt] : cur) {
auto [x, y] = j;
if(a[x][y] == ans[i]) {
if(x+1 != n) nxt[{x+1, y}] += cnt;
if(y+1 != m) nxt[{x, y+1}] += cnt;
}
}
cur = nxt;
nxt.clear();
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |