#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 30;
string grid[N];
ll dp[N][N][2];
int n, m;
const ll INF = 1e18;
void add (ll &a, ll b) {
a += b;
if (a > INF)
a = INF;
}
bool check (string &s, ll k) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
for (int k = 0; k < 2; k++)
dp[i][j][k] = 0;
if (grid[0][0] < s[0])
dp[0][0][1] = 1;
if (grid[0][0] == s[0])
dp[0][0][0] = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (s[i + j] > grid[i][j]) {
if (i)
add (dp[i][j][1], dp[i - 1][j][0]), add (dp[i][j][1], dp[i - 1][j][1]);
if (j)
add (dp[i][j][1], dp[i][j - 1][0]), add (dp[i][j][1], dp[i][j - 1][1]);
}
else {
if (i) {
if (s[i + j] == grid[i][j])
add (dp[i][j][0], dp[i - 1][j][0]);
add (dp[i][j][1], dp[i - 1][j][1]);
}
if (j) {
if (s[i + j] == grid[i][j])
add (dp[i][j][0], dp[i][j - 1][0]);
add (dp[i][j][1], dp[i][j - 1][1]);
}
}
}
return dp[n - 1][m - 1][0] + dp[n - 1][m - 1][1] >= k;
}
int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> grid[i];
ll k;
cin >> k;
int sz = n + m - 1;
string ans (sz, 'z');
for (int i = 0; i < sz; i++) {
ans[i] = 'a';
while (not check (ans, k))
ans[i]++;
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
3 ms |
364 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
364 KB |
Output is correct |
15 |
Correct |
5 ms |
364 KB |
Output is correct |
16 |
Correct |
4 ms |
364 KB |
Output is correct |
17 |
Correct |
5 ms |
364 KB |
Output is correct |
18 |
Correct |
5 ms |
364 KB |
Output is correct |
19 |
Correct |
5 ms |
364 KB |
Output is correct |
20 |
Correct |
4 ms |
364 KB |
Output is correct |