Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.01 KB | None | 0 0
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <queue>
  7. #include <stack>
  8. #include <bitset>
  9. #include <algorithm>
  10. #include <functional>
  11. #include <numeric>
  12. #include <utility>
  13. #include <sstream>
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <cstdio>
  17. #include <cmath>
  18. #include <cstdlib>
  19. #include <cctype>
  20. #include <string>
  21. #include <cstring>
  22. #include <cstdio>
  23. #include <cmath>
  24. #include <cstdlib>
  25. #include <ctime>
  26. #include <cassert>
  27. #include <climits>
  28. #include <cfloat>
  29.  
  30. using namespace std;
  31.  
  32. class PrefixTree {
  33.     public:
  34.         int getNumNodes(vector <string> words);
  35. };
  36.  
  37. int PrefixTree::getNumNodes(vector <string> words) {
  38.     int n = words.size();
  39.     int c[26];
  40.     vector<string> a[26];
  41.     int ans = 1;
  42.  
  43.     for(int i = 0; i < 26; i++) {
  44.         c[i] = 0;
  45.         for(int j = 0; j < n; j++)
  46.             c[i] += (words[j].find(i+'a') != string::npos);
  47.     }
  48.  
  49.     for(int i = 0; i < n; i++) {
  50.         if(words[i].size() == 0) continue;
  51.         int ch = words[i][0]-'a';
  52.         string s;
  53.         for(int j = 0; j < (int)words[i].size(); j++)
  54.             if(c[ch] < c[words[i][j]-'a'])
  55.                 ch = words[i][j]-'a';
  56.         for(int j = 0, k = 0; j < (int)words[i].size(); j++)
  57.             if(words[i][j] - 'a' != ch || k)
  58.                 s.push_back(words[i][j]);
  59.             else k++;
  60.         a[ch].push_back(s);
  61.     }
  62.  
  63.     for(int i = 0; i < 26; i++)
  64.         ans += a[i].size() ? getNumNodes(a[i]) : 0;
  65.  
  66.     return ans;
  67. }
  68.  
  69. // BEGIN CUT HERE {{{
  70. namespace moj_harness {
  71.     int run_test_case(int);
  72.     void run_test(int casenum = -1, bool quiet = false) {
  73.         if (casenum != -1) {
  74.             if (run_test_case(casenum) == -1 && !quiet) {
  75.                 cerr << "Illegal input! Test case " << casenum << " does not exist." << endl;
  76.             }
  77.             return;
  78.         }
  79.        
  80.         int correct = 0, total = 0;
  81.         for (int i=0;; ++i) {
  82.             int x = run_test_case(i);
  83.             if (x == -1) {
  84.                 if (i >= 100) break;
  85.                 continue;
  86.             }
  87.             correct += x;
  88.             ++total;
  89.         }
  90.        
  91.         if (total == 0) {
  92.             cerr << "No test cases run." << endl;
  93.         } else if (correct < total) {
  94.             cerr << "Some cases FAILED (passed " << correct << " of " << total << ")." << endl;
  95.         } else {
  96.             cerr << "All " << total << " tests passed!" << endl;
  97.         }
  98.     }
  99.    
  100.     int verify_case(int casenum, const int &expected, const int &received, clock_t elapsed) {
  101.         cerr << "Example " << casenum << "... ";
  102.        
  103.         string verdict;
  104.         vector<string> info;
  105.         char buf[100];
  106.        
  107.         if (elapsed > CLOCKS_PER_SEC / 200) {
  108.             sprintf(buf, "time %.2fs", elapsed * (1.0/CLOCKS_PER_SEC));
  109.             info.push_back(buf);
  110.         }
  111.        
  112.         if (expected == received) {
  113.             verdict = "PASSED";
  114.         } else {
  115.             verdict = "FAILED";
  116.         }
  117.        
  118.         cerr << verdict;
  119.         if (!info.empty()) {
  120.             cerr << " (";
  121.             for (int i=0; i<(int)info.size(); ++i) {
  122.                 if (i > 0) cerr << ", ";
  123.                 cerr << info[i];
  124.             }
  125.             cerr << ")";
  126.         }
  127.         cerr << endl;
  128.        
  129.         if (verdict == "FAILED") {
  130.             cerr << "    Expected: " << expected << endl;
  131.             cerr << "    Received: " << received << endl;
  132.         }
  133.        
  134.         return verdict == "PASSED";
  135.     }
  136.  
  137.     int run_test_case(int casenum) {
  138.         switch (casenum) {
  139.         case 0: {
  140.             string words[]            = {"topcoder"};
  141.             int expected__            = 9;
  142.  
  143.             clock_t start__           = clock();
  144.             int received__            = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
  145.             return verify_case(casenum, expected__, received__, clock()-start__);
  146.         }
  147.         case 1: {
  148.             string words[]            = {"topcoder","topcoder"};
  149.             int expected__            = 9;
  150.  
  151.             clock_t start__           = clock();
  152.             int received__            = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
  153.             return verify_case(casenum, expected__, received__, clock()-start__);
  154.         }
  155.         case 2: {
  156.             string words[]            = {"aab","ca"};
  157.             int expected__            = 5;
  158.  
  159.             clock_t start__           = clock();
  160.             int received__            = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
  161.             return verify_case(casenum, expected__, received__, clock()-start__);
  162.         }
  163.         case 3: {
  164.             string words[]            = {"aab","ca","ba"};
  165.             int expected__            = 5;
  166.  
  167.             clock_t start__           = clock();
  168.             int received__            = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
  169.             return verify_case(casenum, expected__, received__, clock()-start__);
  170.         }
  171.         case 4: {
  172.             string words[]            = {"ab","cd","ef"};
  173.             int expected__            = 7;
  174.  
  175.             clock_t start__           = clock();
  176.             int received__            = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
  177.             return verify_case(casenum, expected__, received__, clock()-start__);
  178.         }
  179.         case 5: {
  180.             string words[]            = {"a","aa","aaa"};
  181.             int expected__            = 4;
  182.  
  183.             clock_t start__           = clock();
  184.             int received__            = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
  185.             return verify_case(casenum, expected__, received__, clock()-start__);
  186.         }
  187.  
  188.         // custom cases
  189.  
  190. /*      case 6: {
  191.             string words[]            = ;
  192.             int expected__            = ;
  193.  
  194.             clock_t start__           = clock();
  195.             int received__            = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
  196.             return verify_case(casenum, expected__, received__, clock()-start__);
  197.         }*/
  198. /*      case 7: {
  199.             string words[]            = ;
  200.             int expected__            = ;
  201.  
  202.             clock_t start__           = clock();
  203.             int received__            = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
  204.             return verify_case(casenum, expected__, received__, clock()-start__);
  205.         }*/
  206. /*      case 8: {
  207.             string words[]            = ;
  208.             int expected__            = ;
  209.  
  210.             clock_t start__           = clock();
  211.             int received__            = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
  212.             return verify_case(casenum, expected__, received__, clock()-start__);
  213.         }*/
  214.         default:
  215.             return -1;
  216.         }
  217.     }
  218. }
  219.  
  220. int main(int argc, char *argv[]) {
  221.     if (argc == 1) {
  222.         moj_harness::run_test();
  223.     } else {
  224.         for (int i=1; i<argc; ++i)
  225.             moj_harness::run_test(atoi(argv[i]));
  226.     }
  227. }
  228. // }}} END CUT HERE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement