/* Ajay Jadhav */
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <deque>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <string.h>
#include <climits>
#include <cstring>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
#define endl '\n'
void solve()
{
int n, m;
cin >> n >> m;
vector<string> g(n);
rep(i, 0, n)
{
cin >> g[i];
}
vvi dp(n, vi(m, 0));
vector<vector<char>> dir(n, vector<char>(m, 'x'));
dp[0][0] = (g[0][0] - 'a');
dir[0][0] = 'u';
rep(i, 1, n)
{
dp[i][0] = dp[i - 1][0] + (g[i][0] - 'a');
dir[i][0] = 'u';
}
rep(i, 1, m)
{
dp[0][i] = dp[0][i - 1] + (g[0][i] - 'a');
dir[0][i] = 'l';
}
rep(i, 1, n)
{
rep(j, 1, m)
{
if (dp[i - 1][j] < dp[i][j - 1])
{
dp[i][j] += dp[i - 1][j] + (g[i][j] - 'a');
dir[i][j] = 'u';
}
else
{
dp[i][j] += dp[i][j - 1] + (g[i][j] - 'a');
dir[i][j] = 'l';
}
}
}
string ans;
int i = n - 1, j = m - 1;
while (i >= 0 and j >= 0)
{
ans += g[i][j];
if (dir[i][j] == 'u')
i--;
else
j--;
}
reverse(all(ans));
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
while (t--)
{
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
324 KB |
Output isn't correct |
6 |
Incorrect |
3 ms |
2004 KB |
Output isn't correct |
7 |
Incorrect |
13 ms |
9708 KB |
Output isn't correct |
8 |
Incorrect |
40 ms |
28196 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
12 |
Incorrect |
4 ms |
2712 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
1856 KB |
Output isn't correct |
14 |
Incorrect |
37 ms |
28240 KB |
Output isn't correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
11 ms |
11300 KB |
Output is correct |