# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
473146 | MamdouhN | ZigZag (COCI17_zigzag) | C++17 | 2079 ms | 14252 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
vector<string>words;
map<string,int>done;
int n,q;
bool comp(string a,string b)
{
if(a[0]!=b[0])return a<b;
if(done[a]==done[b])return a<b;
return done[a]<done[b];
}
string bs(char c)
{
int st=0,en=words.size();
int mid;
int x = -1;
while(st<en)
{
mid=(st+en)/2;
if(words[mid][0]>c)en = mid-1;
if(words[mid][0]<c)st = mid+1;
if(words[mid][0]==c)
{
if(x==-1)x=mid;
en =mid;
}
}
if(x==-1)x=mid;
mid=(st+en)/2;
done[words[mid]]++;
string ret = words[mid];
//for(auto v: words)cout<<v<<" ";
//cout<<endl;
//cout<<mid<<" "<<x<<endl;
int z = mid+(x-mid)+2;
if(z>words.size())z = mid;
//cout<<mid<<" "<<z<<endl;
sort(words.begin()+mid,words.begin()+z,comp);
//for(auto v: words)cout<<v<<" ";
//cout<<endl;
return ret;
}
main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
for(int i=0;i<n;i++)
{
string x;
cin>>x;
words.push_back(x);
}
sort(words.begin(),words.end(),comp);
while(q--)
{
char c;
cin>>c;
cout<<bs(c)<<endl;
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |