#include<iostream>
using namespace std;
#define int long long
const int INF = 1e18;
int n,m,k;
string s;
char mat[35][35];
int dp[35][35][2];
int calc()
{
dp[1][1][1] = 1;
dp[1][1][0] = 0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(i==1 && j==1)
continue;
if(mat[i][j]==s[i+j-1])
{
dp[i][j][1] = min(INF+5, dp[i-1][j][1] + dp[i][j-1][1]);
dp[i][j][0] = min(INF+5, dp[i-1][j][0] + dp[i][j-1][0]);
}
else if(mat[i][j] < s[i+j-1])
{
dp[i][j][1] = 0;
dp[i][j][0] = min(INF+5, dp[i-1][j][0] + dp[i-1][j][1] + dp[i][j-1][0] + dp[i][j-1][1]);
}
else
{
dp[i][j][1] = 0;
dp[i][j][0] = min(INF+5, dp[i-1][j][0] + dp[i][j-1][0]);
}
}
}
return min(INF+5, dp[n][m][0]);
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>mat[i][j];
}
}
cin>>k;
s = "#";
s.push_back(mat[1][1]);
for(int i=2;i<=n+m-1;i++)
{
s.push_back('a'-1);
}
for(int i=2;i<=n+m-1;i++)
{
for(int j=26-1;j>=0;j--)
{
s[i] = j + 'a';
if(calc() <= k-1)
{
break;
}
}
}
for(int i=1;i<=n+m-1;i++)
cout<<s[i];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
452 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |