Я решаю Таблицу контестов (UVA (10258), P C (110207)) и написал код на C ++ так:
#include <iostream>
#include <utility>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <cstdlib>
#include <algorithm>
//100 contestants, 9 problems.
//data need to store for each contestant: num of problem solved, time consumed.
//array of contestants' data should be sorted by those two values. <- vector of pairs
//problem solved <- set
//time consumed <- check for contestant and L for each line and add them up.
std::vector< std::pair<int, std::pair<int, int> > > contestants;
std::vector<std::set<int>> problem_solved; //store contestants' solved problems
void add_entry(std::string line)
{
int begin = 0;
int end = line.find(" ", begin);
int contestant = std::atoi(line.substr(0, end - begin).c_str());
begin = end + 1;
end = line.find(" ", begin);
int problem = atoi(line.substr(begin, end - begin).c_str());
begin = end + 1;
end = line.find(" ", begin);
int time = atoi(line.substr(begin, end - begin).c_str());
char status = line.back();
switch (status)
{
case 'C':
{
if (!problem_solved[contestant].count(problem))
{
problem_solved[contestant].insert(problem);
}
break;
}
case 'I':
time = 20;
break;
default:
time = 0;
break;
}
contestants[contestant].first = contestant;
contestants[contestant].second.second += time;
}
bool comp(std::pair<int, std::pair<int, int>> a, std::pair<int, std::pair<int, int>> b)
{
if (a.second.first == b.second.first)
{
return (a.second.second <= b.second.second);
}
else
{
return (a.second.first > b.second.first);
}
}
void calc_results()
{
for (int i = 0; i < 100; i++)
{
contestants[i].second.first = problem_solved[i].size();
}
std::sort(contestants.begin(), contestants.end(), comp);
}
int main()
{
//initialize it so that contestants with no records can be ruled out
for (int i = 0; i < 100; i++)
{
contestants.push_back(std::make_pair(0, std::make_pair(0, 0)));
}
//initialization
for (int i = 0; i < 100; i++)
{
problem_solved.push_back(std::set<int>{});
}
std::string line_1;
int num_case;
std::getline(std::cin, line_1);
//get number of cases
num_case = atoi(line_1.c_str());
//get one blank line
std::string blank;
std::getline(std::cin, blank);
//get num_case 's cases.
int i = 0;
std::string line_2;
while (i < num_case)
{
//get unknown number of entries
std::getline(std::cin, line_2);
//move to next case
if (line_2.empty())
{
i++;
}
else
{
//handle one line of entry
add_entry(line_2);
}
}
//calculate final results
calc_results();
//print results
for (int i = 0; i < 100; i++)
{
std::cout << i << "\n";
if (contestants[i].second.first == 0)
{
continue;
}
else
{
std::cout << contestants[i].first << " " << contestants[i].second.first << " " << contestants[i].second.second << "\n";
}
}
return 0;
}
Когда я запускаю отладку, она ведет себя нормально:
1 2 66
3 1 11
Но когда я запускаю исполняемый файл напрямую, вывод выглядит так:
some large integers some large integers some large integers
1 2 66
3 1 11
Затем программа вылетает, показывая «Процесс вернул -1073741819».
I подозреваю, что это какая-то утечка памяти, но не могу понять, что произошло. Кто-нибудь может мне помочь? Спасибо!