#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);}
int n, m, k;
char a[N];
string S[N];
int dp[N];
vector<int> edges[N];
bool used[N];
string ans;
void dps(int u){
used[u] = true;
string mx = "}";
S[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];
mx = min(mx, S[v]);
}
S[u] += mx;
}
void dfs(int u){
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){
if(S[edges[u][0]] > S[edges[u][1]])swap(edges[u][0], edges[u][1]);
int f = edges[u][0];
int s = edges[u][1];
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 << S[i*m+j] << ' ';
}cout << '\n';
}*/
cin >> k;
memset(used, 0, sizeof(used));
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;
}
# |
Verdict |
Execution time |
Memory |
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 |
364 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |