Skip to content

Instantly share code, notes, and snippets.

@danicat
Created January 6, 2014 15:02
Show Gist options
  • Save danicat/8284021 to your computer and use it in GitHub Desktop.
Save danicat/8284021 to your computer and use it in GitHub Desktop.
TopCoder SRM 502 Division 2 500 pts
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
bool compare(string a, string b) {
return a.length() < b.length();
}
class TheLotteryBothDivs {
public:
double find(vector<string> goodSuffixes) {
double prob = 0;
sort(goodSuffixes.begin(), goodSuffixes.end(), compare);
goodSuffixes.resize( distance( goodSuffixes.begin(), unique(goodSuffixes.begin(), goodSuffixes.end()) ) );
vector<string> suffixes = filter(goodSuffixes);
for(auto p : suffixes) {
prob += pow(0.1, p.length());
}
return prob;
}
vector<string> filter(vector<string> suffixes) {
vector<string> filtered;
for(auto p : suffixes) {
bool found = false;
for(auto q : filtered) {
int pos = p.rfind(q);
if( (pos != string::npos) && pos == (p.length()-q.length()) ) {
found = true;
break;
}
}
if( !found )
filtered.push_back(p);
}
return filtered;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment