Created
January 6, 2014 15:02
-
-
Save danicat/8284021 to your computer and use it in GitHub Desktop.
TopCoder SRM 502 Division 2 500 pts
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
#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