File Annotation
Not logged in
4fd800b3a8 2011-02-23        kinaba: #include <iostream>
4fd800b3a8 2011-02-23        kinaba: #include <sstream>
4fd800b3a8 2011-02-23        kinaba: #include <iomanip>
4fd800b3a8 2011-02-23        kinaba: #include <vector>
4fd800b3a8 2011-02-23        kinaba: #include <string>
4fd800b3a8 2011-02-23        kinaba: #include <map>
4fd800b3a8 2011-02-23        kinaba: #include <set>
4fd800b3a8 2011-02-23        kinaba: #include <algorithm>
4fd800b3a8 2011-02-23        kinaba: #include <numeric>
4fd800b3a8 2011-02-23        kinaba: #include <iterator>
4fd800b3a8 2011-02-23        kinaba: #include <functional>
4fd800b3a8 2011-02-23        kinaba: #include <complex>
4fd800b3a8 2011-02-23        kinaba: #include <queue>
4fd800b3a8 2011-02-23        kinaba: #include <stack>
4fd800b3a8 2011-02-23        kinaba: #include <cmath>
4fd800b3a8 2011-02-23        kinaba: #include <cassert>
4fd800b3a8 2011-02-23        kinaba: #include <cstring>
4fd800b3a8 2011-02-23        kinaba: using namespace std;
4fd800b3a8 2011-02-23        kinaba: typedef long long LL;
4fd800b3a8 2011-02-23        kinaba: typedef complex<double> CMP;
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: class InternetSecurity { public:
4fd800b3a8 2011-02-23        kinaba: 	vector <string> determineWebsite(vector <string> address, vector <string> keyword, vector <string> dangerous, int threshold)
4fd800b3a8 2011-02-23        kinaba: 	{
4fd800b3a8 2011-02-23        kinaba: 		int N = address.size();
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		vector< set<string> > kwd(N);
4fd800b3a8 2011-02-23        kinaba: 		for(int i=0; i<N; ++i) {
4fd800b3a8 2011-02-23        kinaba: 			stringstream ss(keyword[i]);
4fd800b3a8 2011-02-23        kinaba: 			for(string s; ss >> s; )
4fd800b3a8 2011-02-23        kinaba: 				kwd[i].insert(s);
4fd800b3a8 2011-02-23        kinaba: 		}
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		set<string> dng(dangerous.begin(), dangerous.end());
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		vector<bool> is_danger(N);
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		bool changed = true;
4fd800b3a8 2011-02-23        kinaba: 		while( changed )
4fd800b3a8 2011-02-23        kinaba: 		{
4fd800b3a8 2011-02-23        kinaba: 			changed = false;
4fd800b3a8 2011-02-23        kinaba: 			for(int i=0; i<N; ++i)
4fd800b3a8 2011-02-23        kinaba: 				if( !is_danger[i] )
4fd800b3a8 2011-02-23        kinaba: 				{
4fd800b3a8 2011-02-23        kinaba: 					vector<string> buf;
4fd800b3a8 2011-02-23        kinaba: 					set_intersection( kwd[i].begin(), kwd[i].end(), dng.begin(), dng.end(), back_inserter(buf) );
4fd800b3a8 2011-02-23        kinaba: 					if( buf.size() >= threshold )
4fd800b3a8 2011-02-23        kinaba: 					{
4fd800b3a8 2011-02-23        kinaba: 						changed = true;
4fd800b3a8 2011-02-23        kinaba: 						dng.insert(kwd[i].begin(), kwd[i].end());
4fd800b3a8 2011-02-23        kinaba: 						is_danger[i] = true;
4fd800b3a8 2011-02-23        kinaba: 					}
4fd800b3a8 2011-02-23        kinaba: 				}
4fd800b3a8 2011-02-23        kinaba: 		}
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: 		vector<string> result;
4fd800b3a8 2011-02-23        kinaba: 		for(int i=0; i<N; ++i)
4fd800b3a8 2011-02-23        kinaba: 			if( is_danger[i] )
4fd800b3a8 2011-02-23        kinaba: 				result.push_back( address[i] );
4fd800b3a8 2011-02-23        kinaba: 		return result;
4fd800b3a8 2011-02-23        kinaba: 	}
4fd800b3a8 2011-02-23        kinaba: };
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: // BEGIN CUT HERE
4fd800b3a8 2011-02-23        kinaba: #include <ctime>
4fd800b3a8 2011-02-23        kinaba: double start_time; string timer()
4fd800b3a8 2011-02-23        kinaba:  { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
4fd800b3a8 2011-02-23        kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
4fd800b3a8 2011-02-23        kinaba:  { os << "{ ";
4fd800b3a8 2011-02-23        kinaba:    for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
4fd800b3a8 2011-02-23        kinaba:    os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
4fd800b3a8 2011-02-23        kinaba: void verify_case(const vector <string>& Expected, const vector <string>& Received) {
4fd800b3a8 2011-02-23        kinaba:  bool ok = (Expected == Received);
4fd800b3a8 2011-02-23        kinaba:  if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
4fd800b3a8 2011-02-23        kinaba:  cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } }
4fd800b3a8 2011-02-23        kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
4fd800b3a8 2011-02-23        kinaba: #define END	 verify_case(_, InternetSecurity().determineWebsite(address, keyword, dangerous, threshold));}
4fd800b3a8 2011-02-23        kinaba: int main(){
4fd800b3a8 2011-02-23        kinaba: 
4fd800b3a8 2011-02-23        kinaba: CASE(0)
4fd800b3a8 2011-02-23        kinaba: 	string address_[] = {"www.topcoder.com",
4fd800b3a8 2011-02-23        kinaba: "www.sindicate_of_evil.com",
4fd800b3a8 2011-02-23        kinaba: "www.happy_citizens.com"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	string keyword_[] = {"hack encryption decryption internet algorithm",
4fd800b3a8 2011-02-23        kinaba: "signal interference evil snake poison algorithm",
4fd800b3a8 2011-02-23        kinaba: "flower baloon topcoder blue sky sea"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> keyword(keyword_, keyword_+sizeof(keyword_)/sizeof(*keyword_));
4fd800b3a8 2011-02-23        kinaba: 	string dangerous_[] = {"hack","encryption","decryption","interference","signal","internet"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> dangerous(dangerous_, dangerous_+sizeof(dangerous_)/sizeof(*dangerous_));
4fd800b3a8 2011-02-23        kinaba: 	int threshold = 3;
4fd800b3a8 2011-02-23        kinaba: 	string __[] = {"www.topcoder.com", "www.sindicate_of_evil.com" };
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> _(__, __+sizeof(__)/sizeof(*__));
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(1)
4fd800b3a8 2011-02-23        kinaba: 	string address_[] = {"brokenlink","flowerpower.net","purchasedomain.com"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	string keyword_[] = {"broken","rose tulips","cheap free domain biggest greatest"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> keyword(keyword_, keyword_+sizeof(keyword_)/sizeof(*keyword_));
4fd800b3a8 2011-02-23        kinaba: 	string dangerous_[] = {"biggest","enemy","hideout"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> dangerous(dangerous_, dangerous_+sizeof(dangerous_)/sizeof(*dangerous_));
4fd800b3a8 2011-02-23        kinaba: 	int threshold = 2;
4fd800b3a8 2011-02-23        kinaba: 	vector <string> _;
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(2)
4fd800b3a8 2011-02-23        kinaba: 	string address_[] = {"a..a.ab.","...aa.b"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	string keyword_[] = {"a bc def","def ghij klmno"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> keyword(keyword_, keyword_+sizeof(keyword_)/sizeof(*keyword_));
4fd800b3a8 2011-02-23        kinaba: 	string dangerous_[] = {"a","b","c","d","e"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> dangerous(dangerous_, dangerous_+sizeof(dangerous_)/sizeof(*dangerous_));
4fd800b3a8 2011-02-23        kinaba: 	int threshold = 1;
4fd800b3a8 2011-02-23        kinaba: 	string __[] = {"a..a.ab.", "...aa.b" };
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> _(__, __+sizeof(__)/sizeof(*__));
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(3)
4fd800b3a8 2011-02-23        kinaba: 	string address_[] = {"www.tsa.gov"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	string keyword_[] = {"information assurance signal intelligence research"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> keyword(keyword_, keyword_+sizeof(keyword_)/sizeof(*keyword_));
4fd800b3a8 2011-02-23        kinaba: 	string dangerous_[] = {"signal","assurance","penguin"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> dangerous(dangerous_, dangerous_+sizeof(dangerous_)/sizeof(*dangerous_));
4fd800b3a8 2011-02-23        kinaba: 	int threshold = 2;
4fd800b3a8 2011-02-23        kinaba: 	string __[] = {"www.tsa.gov" };
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> _(__, __+sizeof(__)/sizeof(*__));
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: CASE(4)
4fd800b3a8 2011-02-23        kinaba: string address_[] = {"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	  string keyword_[] = {"a b c d e f g h i j k l m n o p q r s t u v w x y",
4fd800b3a8 2011-02-23        kinaba: 		  "A B C D E F G H I J K L M N O P Q R S T U V W X Y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y","a b c d e f g h i j k l m n o p q r s t u v w x y"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> keyword(keyword_, keyword_+sizeof(keyword_)/sizeof(*keyword_));
4fd800b3a8 2011-02-23        kinaba: 	  string dangerous_[] = {"a","b","c"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> dangerous(dangerous_, dangerous_+sizeof(dangerous_)/sizeof(*dangerous_));
4fd800b3a8 2011-02-23        kinaba: 	int threshold = 3;
4fd800b3a8 2011-02-23        kinaba: 	string __[] = {"XXX"};
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> _(__, __+sizeof(__)/sizeof(*__));
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: /*
4fd800b3a8 2011-02-23        kinaba: CASE(5)
4fd800b3a8 2011-02-23        kinaba: 	string address_[] = ;
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> address(address_, address_+sizeof(address_)/sizeof(*address_));
4fd800b3a8 2011-02-23        kinaba: 	string keyword_[] = ;
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> keyword(keyword_, keyword_+sizeof(keyword_)/sizeof(*keyword_));
4fd800b3a8 2011-02-23        kinaba: 	string dangerous_[] = ;
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> dangerous(dangerous_, dangerous_+sizeof(dangerous_)/sizeof(*dangerous_));
4fd800b3a8 2011-02-23        kinaba: 	int threshold = ;
4fd800b3a8 2011-02-23        kinaba: 	string __[] = ;
4fd800b3a8 2011-02-23        kinaba: 	  vector <string> _(__, __+sizeof(__)/sizeof(*__));
4fd800b3a8 2011-02-23        kinaba: END
4fd800b3a8 2011-02-23        kinaba: */
4fd800b3a8 2011-02-23        kinaba: }
4fd800b3a8 2011-02-23        kinaba: // END CUT HERE