Skip to content

Commit 3f4207f

Browse files
committed
srm 205 div 1 250
1 parent b3c040a commit 3f4207f

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

SpamDetector.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

SpamDetector.txt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
PROBLEM STATEMENT
2+
You are writing part of a spam detection system. Your job is to analyze the subject lines of e-mail messages and return a count of known spam signalling keywords in the subject lines. Your task is made more difficult by the spammers who try to hide the keywords in several ways. Here we will consider just one obfuscation technique: duplicating characters. Duplicating characters means taking an existing character in a word and inserting more copies of that character into the same place in the word. This process can then be repeated on a different character in the word. The spam signalling keyword "credit" might be modified to "creddiT", "CredittT" or "ccrreeeddiitt", etc., but not "credict".
3+
4+
For the purposes of this problem we will consider subject lines which contain only letters and spaces. The "words" in the subject line are delimited by spaces. A word in the subject line is considered a "match" if the entire word is the same as at least one entire keyword, after possibly removing some duplicated characters from the subject word. A keyword that matches only part of a subject word or a subject word that matches only part of a keyword does not count. Note that if a keyword contains a double letter, the subject word must also contain (at least) a double letter in the same position to match ("double letter" means two consecutive letters in the word that are the same). For this application, all matches (and the use of the term "same") are case insensitive.
5+
6+
Given a subject line and a list of keywords, return the count of words in the subject line which "match" words in the keyword list. If multiple words in the subject line match the same keyword, they are each counted, but a word in the subject line that matches multiple keywords is only counted once.
7+
8+
9+
10+
DEFINITION
11+
Class:SpamDetector
12+
Method:countKeywords
13+
Parameters:string, vector <string>
14+
Returns:int
15+
Method signature:int countKeywords(string subjectLine, vector <string> keywords)
16+
17+
18+
CONSTRAINTS
19+
-subjectLine will contain between 0 and 50 characters, inclusive.
20+
-subjectLine will include only letter ('a' to 'z' and 'A' to 'Z') and space (' ') characters.
21+
-keywords will have between 0 and 50 elements, inclusive.
22+
-each element of keywords will contain between 1 and 50 characters, inclusive.
23+
-each element of keywords will consist of only letters ('a' to 'z' and 'A' to 'Z').
24+
-The same letter (ignoring case) never appears more than twice consecutively in any element of keywords. (ie. "aabbAAbb" is ok, but "aaAbb" is not allowed.)
25+
26+
27+
EXAMPLES
28+
29+
0)
30+
"LoooW INTEREST RATES available dont BE slow"
31+
{"interest","rates","loan","available","LOW"}
32+
33+
Returns: 4
34+
35+
"INTEREST" , "RATES" , "available", and "LoooW" match. Note that "slow" does not match, even though it contains the substring "low" which is a keyword.
36+
37+
1)
38+
"Dear Richard Get Rich Quick no risk"
39+
{"rich","risk","Quicken","wealth","SAVE"}
40+
41+
Returns: 2
42+
43+
Don't match "Richard"
44+
45+
2)
46+
"in debbtt againn and aAgain and AGAaiIN"
47+
{"AGAIN","again","Again","again"}
48+
49+
Returns: 3
50+
51+
3)
52+
"PlAyy ThEE Lottto get Loottoo feever"
53+
{"play","lotto","lottery","looser"}
54+
55+
Returns: 3
56+
57+
4)
58+
" "
59+
{"empty","space","does","not","match"}
60+
61+
Returns: 0

0 commit comments

Comments
 (0)