#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) begin(x),end(x)
#define ld long double
#define SZ(x) (int)(x).size()
#define mem(x,y) memset(&x,y,sizeof(x))
using namespace std;
const int n0=33;
const ll inf=1e18+123;
int n,m;
char c[n0][n0],s[n0+n0];
void add(ll &x,ll y) {
x=min(inf,x+y);
}
ll dp[n0][n0][2];
ll get() {
mem(dp,0);
if(c[1][1]<=s[1])
dp[1][1][(c[1][1]<s[1])]=1;
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
for(int k=0; k<2; k++) {
if(k) {
if(i<n) {
add(dp[i+1][j][1],dp[i][j][1]);
}
if(j<m) {
add(dp[i][j+1][1],dp[i][j][1]);
}
} else {
if(i<n && c[i+1][j]<=s[i+j]) {
add(dp[i+1][j][(c[i+1][j]<s[i+j])],dp[i][j][0]);
}
if(j<m && c[i][j+1]<=s[i+j]) {
add(dp[i][j+1][(c[i][j+1]<s[i+j])],dp[i][j][0]);
}
}
}
}
}
return dp[n][m][1];
}
int main() {
ios_base::sync_with_stdio(0),cin.tie(0);
cin >> n >> m;
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
cin >> c[i][j];
}
}
ll k;
cin >> k;
int len=n+m-1;
for(int i=1; i<=len; i++) s[i]='$';
for(int i=1; i<=len; i++) {
int last=-1;
for(int j=0; j<26; j++) {
s[i]=char('a'+j);
if(get()<k) last=j;
}
assert(last!=-1);
s[i]=char('a'+last);
}
for(int i=1; i<=len; i++) cout << s[i];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
7 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
7 ms |
332 KB |
Output is correct |
13 |
Correct |
9 ms |
332 KB |
Output is correct |
14 |
Correct |
9 ms |
332 KB |
Output is correct |
15 |
Correct |
16 ms |
324 KB |
Output is correct |
16 |
Correct |
16 ms |
340 KB |
Output is correct |
17 |
Correct |
15 ms |
336 KB |
Output is correct |
18 |
Correct |
17 ms |
340 KB |
Output is correct |
19 |
Correct |
15 ms |
332 KB |
Output is correct |
20 |
Correct |
14 ms |
336 KB |
Output is correct |