#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
const int N = 3e3 + 7;
long long dp[N][N];
string a[N];
long long C[N];
int main() {
//freopen("input.txt", "r", stdin);
int n, m;
cin >> n >> m;
for (int i = n - 1; i >= 0; i--)
for (int j = m - 1; j >= 0; j--) {
if (i == n - 1 && j == m - 1)dp[i][j] = 1;
else dp[i][j] = dp[i + 1][j] + dp[i][j + 1];
}
for (int i = 0; i < n; ++i)cin >> a[i];
long long k;
cin >> k;
string ans = "";
set<pair<int, int>> v,cur;
v.insert({ 0,0 });
for (int i = 0; i < (n + m)-1; ++i) {
for (int j = 0; j < 26; ++j)C[j] = 0;
if (i == 0) {
ans += a[v.begin()->first][v.begin()->second];
}
for (auto to:v) {
if (to.first + 1 < n) {
C[a[to.first + 1][to.second]-'a'] += dp[to.first+ 1][to.second];
}
if (to.second + 1 < m) {
C[a[to.first][to.second + 1]-'a'] += dp[to.first][to.second + 1];
}
}
for (int j = 0; j < 26; ++j) {
if (C[j] < k) {
k -= C[j];
}
else {
ans += char(j + 'a');
cur.clear();
for (auto to:v) {
if (to.first + 1 < n&&a[to.first + 1][to.second] - 'a' == j) {
cur.insert({ to.first + 1,to.second });
}
if (to.second + 1 < m&&a[to.first][to.second + 1] - 'a' == j) {
cur.insert({ to.first,to.second + 1 });
}
}
v = cur;
break;
}
}
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Incorrect |
2 ms |
544 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |