Created
October 12, 2011 00:01
-
-
Save acadavid/1279835 to your computer and use it in GitHub Desktop.
CondorcetVoting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ranks(const string &v, map<char, int> &m){ | |
for(int i=0; i<v.size(); ++i) { | |
m[v[i]] = -1; | |
} | |
int c=0; | |
for(map<char, int>::iterator it = m.begin(); it != m.end(); it++) { | |
it->second = c++; | |
} | |
} | |
int winner(vector<string> votes) { | |
vector<int> rankings (votes[0].size(), 0); | |
for(int i=0; i<votes.size(); ++i) { | |
map<char, int> r; | |
ranks(votes[i], r); | |
for(int j = 0; votes[i].size(); ++j) { | |
rankings[j] += r[votes[i][j]]; | |
} | |
} | |
int winner_index = min(rankings.begin(), rankings.end()) - rankings.begin(); | |
int winner_val = rankings[winner_index]; | |
int c_rep = 0; | |
for(int i=0; i<rankings.size(); i++) { | |
c_rep += winner_val == rankings[i]; | |
} | |
if (c_rep > 1) return -1; | |
return winner_index; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment