How i must output anwser?
Написал лобовой вариант и всё равно WA10! Значит, вывод не верен. Кто может подсказать, как выводить ответ?(Форум не помог)
#define _CRT_SECURE_NO_WARNINGS
#define mk make_pair
#include <string>
#include <map>
#include <iostream>
using namespace std;
typedef map <string, int> mymap;
const int nmax = 100005;
mymap t[4 * nmax];
mymap answer[nmax];
string word[nmax];
int cnt[nmax];
int n, m;
string w_print[11];
int c_print[11];
mymap Search(int L, int R);
int BinaryDown(int L, int R, string s);
int BinaryUp(int L, int R, string s);
mymap Optimal(mymap map1, mymap map2);
void Print(mymap M);
void QSort_W(int L, int R);
void QSort2(int L, int R);
void QSort3(int L, int R);
int main()
{
cin >> n;
for (int i = 1; i <= n; i++) cin >> word[i] >> cnt[i];
QSort_W(1, n);
cin >> m;
for (int i = 1; i <= m; i++)
{
string s;
cin >> s;
int L, R;
L = BinaryDown(1, n, s);
R = BinaryUp(1, n, s);
answer[i].clear();
if (L != -1 && R != -1) answer[i] = Search(L, R);
}
for (int i = 1; i <= m; i++)
{
Print(answer[i]);
if(i!=m) cout<<endl;
if(!(answer[i].empty()) && i!=m) cout<<endl;
}
return 0;
}
mymap Search(int L, int R)
{
mymap res;
res.clear();
for(int i=L;i<=R;i++)
{
mymap x;
x.insert(mk(word[i],cnt[i]));
res=Optimal(res,x);
}
return res;
}
void Print(mymap M)
{
int n = 0;
for (mymap::iterator it = M.begin(); it != M.end(); it++)
{
w_print[++n] = it->first;
c_print[n] = it->second;
}
if (n == 0) return;
QSort2(1, n);
for (int i = 1; i <= n;)
{
int j = i + 1;
while (j <= n && c_print[j] == c_print[i]) j++;
QSort3(i, j - 1);
i = j;
}
for (int i = 1; i<n; i++) cout << w_print[i] << endl;
cout << w_print[n];
}
void QSort2(int L, int R)
{
int i, j;
int X = c_print[(L + R) / 2];
i = L, j = R;
while (i <= j)
{
while (c_print[i] > X) i++;
while (c_print[j] < X) j--;
if (i <= j)
{
string Y = w_print[i]; w_print[i] = w_print[j]; w_print[j] = Y;
int k = c_print[i]; c_print[i] = c_print[j]; c_print[j] = k;
i++;
j--;
}
}
if (L < j) QSort2(L, j);
if (i < R) QSort2(i, R);
}
void QSort3(int L, int R)
{
int i, j;
string X = w_print[(L + R) / 2];
i = L, j = R;
while (i <= j)
{
while (w_print[i] < X) i++;
while (w_print[j] > X) j--;
if (i <= j)
{
string Y = w_print[i]; w_print[i] = w_print[j]; w_print[j] = Y;
int k = c_print[i]; c_print[i] = c_print[j]; c_print[j] = k;
i++;
j--;
}
}
if (L < j) QSort3(L, j);
if (i < R) QSort3(i, R);
}
void QSort_W(int L, int R)
{
int i, j;
string X = word[(L + R) / 2];
i = L, j = R;
while (i <= j)
{
while (word[i] < X) i++;
while (word[j] > X) j--;
if (i <= j)
{
string Y = word[i]; word[i] = word[j]; word[j] = Y;
int k = cnt[i]; cnt[i] = cnt[j]; cnt[j] = k;
i++;
j--;
}
}
if (L < j) QSort_W(L, j);
if (i < R) QSort_W(i, R);
}
int BinaryDown(int L, int R, string s)
{
for(int i=L;i<=R;i++) if(s==word[i].substr(0, s.length())) return i;
return -1;
}
int BinaryUp(int L, int R, string s)
{
for(int i=R;i>=L;i--) if(s==word[i].substr(0, s.length())) return i;
return -1;
}
mymap Optimal(mymap map1, mymap map2)
{
mymap res;
res.clear();
if (map1.size() + map2.size() <= 10)
{
res = map1;
for (mymap::iterator it = map2.begin(); it != map2.end(); it++) res.insert(mk(it->first,it->second));
return res;
}
else
{
res = map1;
for (mymap::iterator it = map2.begin(); it != map2.end(); it++)
{
if (res.size() < 10) res.insert(mk(it->first, it->second));
else
{
if (res.begin()->second < it->second)
{
res.erase(res.begin());
res.insert(mk(it->first, it->second));
}
}
}
return res;
}
}