#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
zigzag.cpp: In function 'std::string bs(char)':
zigzag.cpp:41:9: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if(z>words.size())z = mid;
| ~^~~~~~~~~~~~~
zigzag.cpp: At global scope:
zigzag.cpp:48:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
48 | main()
| ^~~~
zigzag.cpp: In function 'std::string bs(char)':
zigzag.cpp:40:9: warning: 'mid' may be used uninitialized in this function [-Wmaybe-uninitialized]
40 | int z = mid+(x-mid)+2;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
308 KB |
Output isn't correct |
3 |
Incorrect |
4 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
5 ms |
332 KB |
Output isn't correct |
5 |
Incorrect |
6 ms |
332 KB |
Output isn't correct |
6 |
Incorrect |
6 ms |
312 KB |
Output isn't correct |
7 |
Execution timed out |
2079 ms |
14132 KB |
Time limit exceeded |
8 |
Execution timed out |
2078 ms |
14220 KB |
Time limit exceeded |
9 |
Execution timed out |
2077 ms |
14252 KB |
Time limit exceeded |
10 |
Execution timed out |
2077 ms |
14240 KB |
Time limit exceeded |