#ifndef Local
#pragma GCC optimize("O3,unroll-loops")
const int lim=2e5+100;
#else
const int lim=1e3+100;
#endif
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
const int mod=1e9+7;
using pii=pair<int,int>;
inline void solve(){
int n,m;
cin>>n>>m;
string s[n];
for(int i=0;i<n;i++){
cin>>s[i];
}
string res={s[0][0]};
set<pii>all{{0,0}},nexty;
for(int i=1;i<n+m-1;i++){
char mini=CHAR_MAX;
for(pii cor:all){
if(cor.first+1<n){
if(s[cor.first+1][cor.second]==mini){
nexty.insert({cor.first+1,cor.second});
}else if(s[cor.first+1][cor.second]<mini){
nexty.clear();
nexty.insert({cor.first+1,cor.second});
mini=s[cor.first+1][cor.second];
}
}
if(cor.second+1<m){
if(s[cor.first][cor.second+1]==mini){
nexty.insert({cor.first,cor.second+1});
}else if(s[cor.first][cor.second+1]<mini){
nexty.clear();
nexty.insert({cor.first,cor.second+1});
mini=s[cor.first][cor.second+1];
}
}
}
res.pb(mini);
swap(nexty,all);
nexty.clear();
}
cout<<res<<"\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
#ifdef Local
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#else
//freopen("grass.in","r",stdin);
//freopen("grass.out","w",stdout);
#endif
int t=1;
//cin>>t;
while (t--)
{
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
1884 KB |
Output is correct |
8 |
Correct |
8 ms |
4556 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
600 KB |
Output is correct |
14 |
Correct |
7 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
47 ms |
2228 KB |
Output is correct |