|
| 1 | +#include <iostream> |
| 2 | +#include <sstream> |
| 3 | +#include <vector> |
| 4 | +#include <algorithm> |
| 5 | +#include <set> |
| 6 | +#include <map> |
| 7 | +#include <queue> |
| 8 | +#include <cstring> |
| 9 | +#include <climits> |
| 10 | +#include <cctype> |
| 11 | +using namespace std; |
| 12 | +typedef pair<int,int> pi; |
| 13 | +typedef vector<int> vi; |
| 14 | +typedef vector<vi> vvi; |
| 15 | +typedef vector<string> vs; |
| 16 | +typedef vector<vs> vvs; |
| 17 | + |
| 18 | +class SpamDetector { |
| 19 | + public: |
| 20 | + vs split(string s){ |
| 21 | + vs r; |
| 22 | + int i = 0, n = s.size(); |
| 23 | + while(i < n){ |
| 24 | + string t; |
| 25 | + char c = s[i]; |
| 26 | + while(i < n && s[i] == c){ |
| 27 | + t.push_back(c); |
| 28 | + i++; |
| 29 | + } |
| 30 | + r.push_back(t); |
| 31 | + } |
| 32 | + return r; |
| 33 | + } |
| 34 | + int match(string a, string b){ |
| 35 | + for(char &c : a) c = tolower(c); |
| 36 | + for(char &c : b) c = tolower(c); |
| 37 | + vs g1 = split(a), g2 = split(b); |
| 38 | + int ok = 1; |
| 39 | + int n1 = g1.size(), n2 = g2.size(); |
| 40 | + if(n1 != n2) return 0; |
| 41 | + for(int i = 0; i < n1; i++){ |
| 42 | + string a1 = g1[i], a2 = g2[i]; |
| 43 | + if(a1[0] != a2[0] || a1.size() < a2.size()) ok = 0; |
| 44 | + } |
| 45 | + return ok; |
| 46 | + } |
| 47 | + int countKeywords(string subjectLine, vector <string> keywords) |
| 48 | + { |
| 49 | + stringstream ss(subjectLine); |
| 50 | + string a; |
| 51 | + int r = 0; |
| 52 | + while(ss>>a){ |
| 53 | + for(string b : keywords){ |
| 54 | + if(match(a, b)){ |
| 55 | + r++; |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + return r; |
| 61 | + } |
| 62 | + |
| 63 | +// BEGIN CUT HERE |
| 64 | + public: |
| 65 | + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } |
| 66 | + private: |
| 67 | + template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } |
| 68 | + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } |
| 69 | + void test_case_0() { string Arg0 = "LoooW INTEREST RATES available dont BE slow"; string Arr1[] = {"interest","rates","loan","available","LOW"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 4; verify_case(0, Arg2, countKeywords(Arg0, Arg1)); } |
| 70 | + void test_case_1() { string Arg0 = "Dear Richard Get Rich Quick no risk"; string Arr1[] = {"rich","risk","Quicken","wealth","SAVE"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; verify_case(1, Arg2, countKeywords(Arg0, Arg1)); } |
| 71 | + void test_case_2() { string Arg0 = "in debbtt againn and aAgain and AGAaiIN"; string Arr1[] = {"AGAIN","again","Again","again"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 3; verify_case(2, Arg2, countKeywords(Arg0, Arg1)); } |
| 72 | + void test_case_3() { string Arg0 = "PlAyy ThEE Lottto get Loottoo feever"; string Arr1[] = {"play","lotto","lottery","looser"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 3; verify_case(3, Arg2, countKeywords(Arg0, Arg1)); } |
| 73 | + void test_case_4() { string Arg0 = " "; string Arr1[] = {"empty","space","does","not","match"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(4, Arg2, countKeywords(Arg0, Arg1)); } |
| 74 | + |
| 75 | +// END CUT HERE |
| 76 | + |
| 77 | + }; |
| 78 | + |
| 79 | +// BEGIN CUT HERE |
| 80 | +int main(){ |
| 81 | + |
| 82 | + SpamDetector ___test; |
| 83 | + ___test.run_test(-1); |
| 84 | + |
| 85 | +} |
| 86 | +// END CUT HERE |
0 commit comments