I keep getting RUNTIME ERROR.
My code passes several test cases from internet and from my own. Can someone please find out a test case that my code produces run time erorr?
Here is my code:
Code: Select all
#include <math.h>
#include <string.h>
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;
typedef pair<string, bool> candidate;
typedef pair<int, int> candidateId;
void tokenizeString(const string &str, vector<string>& tokens, const string &delimiters);
bool candidateIdCompare(const candidateId &c1, const candidateId &c2);
bool isStringEmptyOrWhitespace(string str);
const int CANDIDATE_MAX_COUNT = 20;
const int BALLOT_MAX_COUNT = 1000;
candidate candidates[CANDIDATE_MAX_COUNT];
vector<int> ballots[BALLOT_MAX_COUNT];
bool isCandidateAlive(int candidateId)
{
return candidates[candidateId].second;
};
void popUntilAliveCandidate(vector<int> &candidateIds)
{
while (!candidateIds.empty())
{
int candidateId = candidateIds.front();
if (isCandidateAlive(candidateId))
break;
candidateIds.erase(candidateIds.begin());
}
};
void initializeAliveCandidateBallots(candidateId *curBallots, int aliveCandidateCount)
{
int id = 0;
for (int i = 0; i < aliveCandidateCount; ++i)
{
while (!isCandidateAlive(id))
{
id++;
}
curBallots[i] = candidateId(id++, 0);
}
};
candidateId *getCandidateId(int id, candidateId *curBallots, int aliveCandidateCount)
{
for (int i = 0; i < aliveCandidateCount; ++i)
if (curBallots[i].first == id)
return curBallots + i;
};
int main()
{
//ifstream inputFile("input.txt"); // change to cin
//ofstream outputFile("output.txt");
string input;
getline(cin, input);
int numTest = atoi(input.c_str());
getline(cin, input); // first blank line
for (int k = 1; k <= numTest; k++)
{
memset(candidates, 0, sizeof(candidate) * CANDIDATE_MAX_COUNT);
for (int i = 0; i < BALLOT_MAX_COUNT; ++i)
ballots[i].clear();
while (getline(cin, input))
{
if (!isStringEmptyOrWhitespace(input))
break;
}
int candidateCount = atoi(input.c_str());
for (int i = 0; i < candidateCount; ++i)
{
getline(cin, input);
candidates[i] = candidate(input, true);
}
int ballotCount = 0;
while (getline(cin, input))
{
if (isStringEmptyOrWhitespace(input))
break;
vector<string> ids;
tokenizeString(input, ids, " ");
for (auto it = ids.begin(); it != ids.end(); it++)
ballots[ballotCount].push_back(atoi((*it).c_str()) - 1);
ballotCount++;
}
vector<int> winnerIds;
int aliveCandidateCount = candidateCount;
if (ballotCount == 0)
{
for (int i = 0; i < aliveCandidateCount; ++i)
winnerIds.push_back(i);
}
while (ballotCount > 0)
{
candidateId *curBallots = new candidateId[aliveCandidateCount];
initializeAliveCandidateBallots(curBallots, aliveCandidateCount);
for (int i = 0; i < ballotCount; ++i)
{
getCandidateId(ballots[i].front(), curBallots, aliveCandidateCount)->second++;
}
sort(curBallots, curBallots + aliveCandidateCount, candidateIdCompare);
float voteRatio = (float)curBallots[aliveCandidateCount-1].second / ballotCount;
if (voteRatio > 50)
{
winnerIds.push_back(curBallots[aliveCandidateCount-1].first);
break;
}
else
{
bool isVoteRatioAllTheSame = true;
for (int i = 1; i < aliveCandidateCount; ++i)
{
if (curBallots[0].second != curBallots[i].second)
{
isVoteRatioAllTheSame = false;
break;
}
}
if (isVoteRatioAllTheSame)
{
for (int i = 0; i < aliveCandidateCount; ++i)
{
winnerIds.push_back(curBallots[i].first);
}
break;
}
else
{
int numVotes = curBallots[0].second;
int newlyDeadCandidateCount = 0;
for (int i = 0; i < aliveCandidateCount; ++i)
{
if (curBallots[i].second == numVotes)
{
int deadId = curBallots[i].first;
candidates[deadId].second = false;
newlyDeadCandidateCount++;
}
else
{
break;
}
}
aliveCandidateCount -= newlyDeadCandidateCount;
for (int i = 0; i < ballotCount; ++i)
{
popUntilAliveCandidate(ballots[i]);
}
}
}
}
for (auto it = winnerIds.begin(); it != winnerIds.end(); ++it)
{
cout << candidates[(*it)].first << endl;
}
if (k < numTest)
cout << endl;
}
//inputFile.close();
//outputFile.close();
return 0;
}
void tokenizeString(const string &str, vector<string>& tokens, const string &delimiters = " ")
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (pos != string::npos || lastPos != string::npos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
bool candidateIdCompare(const candidateId &c1, const candidateId &c2)
{
return c1.second < c2.second;
}
bool isStringEmptyOrWhitespace(string str)
{
if (str.empty() || str.find_first_not_of(' ') == std::string::npos)
return true;
return false;
}
I've intentionally put more blank lines to make sure my code works fine in case the real input has more than one blank line by mistake.
Code: Select all
33
3
John Doe
Jane Smith
Sirhan Sirhan
1 2 3
2 1 3
2 3 1
1 2 3
3 1 2
1
John Doe
1
2
John Doe
Jane Smith
1 2
1 2
2
John Doe
Jane Smith
1 2
2 1
2
John Doe
Jane Smith
1 2
2 1
1 2
2 1
2
John Doe
Jane Smith
1 2
2 1
2 1
2 1
1 2
3
John Doe
Jane Smith
Siran Siran
1 2 3
2 3 1
3 1 2
3
John Doe
Jane Smith
Siran Siran
1 2 3
2 1 3
2 3 1
3
John Doe
Jane Smith
Siran Siran
1 2 3
1 2 3
1 2 3
3
John Doe
Jane Smith
Siran Siran
1 2 3
3
John Doe
Jane Smith
Siran Siran
1 2 3
1 2 3
2 1 3
2 1 3
3 1 2
3 2 2
3
John Doe
Jane Smith
Siran Siran
1 2 3
1 2 3
1 2 3
2 1 3
2 1 3
2 1 3
3 1 2
3 2 1
5
John Doe
Jane Smith
Siran Siran
Hoho Haha
Hehe Huhu
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
2 1 3 4 5
2 1 3 4 5
2 1 3 4 5
3 1 2 4 5
3 1 2 4 5
3 1 2 4 5
4 2 1 3 5
4 2 1 3 5
4 2 1 3 5
5 1 2 3 4
5 2 1 3 4
5
John Doe
Jane Smith
Siran Siran
Hoho Haha
Hehe Huhu
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
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3 2 5 4 1
5 2 6 1 3 4
3 6 1 5 2 4
1 2 4 6 3 5
3 2 5 6 1 4
5 6 2 4 3 1
5 3 1 4 2 6
2 4 5 6 3 1
2 5 3 4 6 1
4
jalal
hasan
bijon
saiket
1 2 3 4
3 1 2 4
1 2 3 4
2 1 3 4
3 1 2 4
4 3 1 3
3
John Doe
Jane Smith
Sirhan Sirhan
1 2 3
2 1 3
2 3 1
1 2 3
3 1 2
3
John Doe
Jane Smith
Sirhan Sirhan
1 2 3
2 1 3
2 3 1
1 2 3
3 1 2
4
jalal
hasan
bijon
saiket
1 2 3 4
2 1 3 4
1 2 3 4
2 1 3 4
3 1 2 4
4 2 1 3
12
A
B
C
D
E
F
G
H
I
J
K
L
10 9 12 4 5 2 6 3 8 1 7 11
1 7 2 11 4 3 12 10 8 6 9 5
7 10 11 8 6 2 5 1 12 3 4 9
10 4 9 6 12 1 11 8 5 3 2 7
1 4 6 12 2 11 8 10 7 5 3 9
8 4 9 3 11 7 12 2 5 10 1 6
4 1 7 5 9 12 3 6 8 10 11 2
9 6 12 3 1 11 8 2 7 10 5 4
6 9 1 3 11 2 4 10 12 7 5 8
2 10 9 1 4 6 12 11 8 3 5 7
1 10 3 7 4 8 5 6 11 12 9 2
7 10 1 3 12 2 4 11 8 6 5 9
4 8 1 9 5 10 11 7 6 2 3 12
12 8 1 7 9 4 6 10 2 3 11 5
3 2 9 6 1 4 10 11 7 8 12 5
1 2 7 3 10 12 8 4 11 5 9 6
6 11 10 7 5 8 4 2 1 3 12 9
3 8 12 4 7 1 5 11 9 6 10 2
4 8 10 1 6 3 12 5 7 2 11 9
5 8 10 9 7 4 1 11 12 2 3 6
2 10 6 8 5 1 9 4 7 3 12 11
9 5 7 11 4 1 8 10 2 6 3 12
2 12 7 4 3 5 10 9 6 11 8 1
16
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
5 4 11 1 14 6 2 12 9 16 3 13 15 10 8 7
11 7 1 16 8 10 2 9 14 15 12 5 4 3 13 6
16 6 3 5 1 15 10 14 13 11 2 4 8 12 9 7
14 12 1 6 7 5 16 4 13 10 8 9 2 15 11 3
2 3 10 15 4 13 11 16 9 12 14 5 8 7 6 1
7 11 10 5 8 6 15 14 4 13 2 3 9 16 1 12
Thank you very much,
whiteSkar