Official
F - Concat (2nd) Editorial by en_translator
Supplement to Another Solution 2It is difficult to let a program reference a specific element \(O(N)\) time without knowing the actual code.
In our problem, even if you use the sort function in C++, by shuffle-ing the input array, the pivot is essentially chosen randomly, making it difficult to hack.
The following code is AC (accepted) with high probability, but if you comment out Line 28 and submit it, it will receive a TLE (Time Limit Exceeded) verdict.
Sample code (C++):
#include<bits/stdc++.h>
using namespace std;
bool comp(const string &x,const string &y){
return (x+y < y+x);
}
string concat(vector<string> &s){
string res="";
for(auto &nx : s){ res+=nx; }
return res;
}
int main(){
std::random_device seed_gen;
std::uint32_t seed = seed_gen();
std::mt19937_64 engine(seed);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<string> s(n);
for(auto &nx : s){cin >> nx;}
shuffle(s.begin(),s.end(),engine);
sort(s.begin(),s.end(),comp);
if(n==2){
cout << s[1]+s[0] << "\n";
continue;
}
bool ok=false;
for(int i=1;i<n;i++){
if(s[i-1]+s[i] == s[i]+s[i-1]){ok=true; break;}
}
if(ok){
cout << concat(s) << "\n";
continue;
}
swap(s[n-1],s[n-2]);
string c1=concat(s);
swap(s[n-1],s[n-2]);
swap(s[n-2],s[n-3]);
cout << min(c1,concat(s)) << "\n";
}
return 0;
}
posted:
last update: