#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
template <class T> bool ckmin(T& a, const T& b) {return a > b ? a = b, true : false;}
template <class T> bool ckmax(T& a, const T& b) {return a < b ? a = b, true : false;}
const int mod = 1e9+7, N = 1005;
int msb(int val){return sizeof(int)*8-__builtin_clzll(val)-1;}
int n, m, k;
char a[N];
string Smn[N], Smx[N];
int dp[N];
vector<int> edges[N];
bool used[N];
string ans;
void dps(int u){
used[u] = true;
string mx = "A";
string mn = "}";
Smx[u] += a[u];
Smn[u] += a[u];
if(edges[u].empty()){
dp[u] = 1;
return;
}
for(auto v : edges[u]){
if(!used[v])dps(v);
dp[u] += dp[v];
ckmax(mx, Smx[v]);
ckmin(mn, Smn[v]);
}
Smx[u] += mx;
Smn[u] += mn;
}
void dfs(int u){
// cout << u/n << ' ' << u%m << '\n';
ans.pb(a[u]);
if(edges[u].size() == 0)return;
if(edges[u].size() == 1){
dfs(edges[u][0]);
}else if(edges[u].size() == 2){
int f = edges[u][0];
int s = edges[u][1];
if(Smn[f] > Smn[s])swap(f, s);
else if(Smn[f] == Smn[s]){
if(Smx[f] >= Smx[s])swap(f, s);
else if(Smx[f] == Smx[s]){
// hurts
}
}
if(dp[f] >= k)dfs(f);
else{
k -= dp[f];
dfs(s);
}
}else assert(false);
}
void solve(int test_case){
int i, j;
cin >> n >> m;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cin >> a[i*m+j];
if(i+1<n){
edges[i*m+j].pb((i+1)*m+j);
}
if(j+1 < m){
edges[i*m+j].pb(i*m+j+1);
}
}
}
dps(0);
/* for(i=0;i<n;i++){
for(j=0;j<m;j++){
cout << Smn[i*m+j] << ' ';
}cout << '\n';
}*/
cin >> k;
dfs(0);
cout << ans << '\n';
return;
}
signed main(){
FASTIO;
#define MULTITEST 0
#if MULTITEST
int _T;
cin >> _T;
for(int T_CASE = 1; T_CASE <= _T; T_CASE++)
solve(T_CASE);
#else
solve(1);
#endif
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 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |