Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <list>
- #include <map>
- #include <set>
- #include <deque>
- #include <queue>
- #include <stack>
- #include <bitset>
- #include <algorithm>
- #include <functional>
- #include <numeric>
- #include <utility>
- #include <sstream>
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <cmath>
- #include <cstdlib>
- #include <cctype>
- #include <string>
- #include <cstring>
- #include <cstdio>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- #include <cassert>
- #include <climits>
- #include <cfloat>
- using namespace std;
- class PrefixTree {
- public:
- int getNumNodes(vector <string> words);
- };
- int PrefixTree::getNumNodes(vector <string> words) {
- int n = words.size();
- int c[26];
- vector<string> a[26];
- int ans = 1;
- for(int i = 0; i < 26; i++) {
- c[i] = 0;
- for(int j = 0; j < n; j++)
- c[i] += (words[j].find(i+'a') != string::npos);
- }
- for(int i = 0; i < n; i++) {
- if(words[i].size() == 0) continue;
- int ch = words[i][0]-'a';
- string s;
- for(int j = 0; j < (int)words[i].size(); j++)
- if(c[ch] < c[words[i][j]-'a'])
- ch = words[i][j]-'a';
- for(int j = 0, k = 0; j < (int)words[i].size(); j++)
- if(words[i][j] - 'a' != ch || k)
- s.push_back(words[i][j]);
- else k++;
- a[ch].push_back(s);
- }
- for(int i = 0; i < 26; i++)
- ans += a[i].size() ? getNumNodes(a[i]) : 0;
- return ans;
- }
- // BEGIN CUT HERE {{{
- namespace moj_harness {
- int run_test_case(int);
- void run_test(int casenum = -1, bool quiet = false) {
- if (casenum != -1) {
- if (run_test_case(casenum) == -1 && !quiet) {
- cerr << "Illegal input! Test case " << casenum << " does not exist." << endl;
- }
- return;
- }
- int correct = 0, total = 0;
- for (int i=0;; ++i) {
- int x = run_test_case(i);
- if (x == -1) {
- if (i >= 100) break;
- continue;
- }
- correct += x;
- ++total;
- }
- if (total == 0) {
- cerr << "No test cases run." << endl;
- } else if (correct < total) {
- cerr << "Some cases FAILED (passed " << correct << " of " << total << ")." << endl;
- } else {
- cerr << "All " << total << " tests passed!" << endl;
- }
- }
- int verify_case(int casenum, const int &expected, const int &received, clock_t elapsed) {
- cerr << "Example " << casenum << "... ";
- string verdict;
- vector<string> info;
- char buf[100];
- if (elapsed > CLOCKS_PER_SEC / 200) {
- sprintf(buf, "time %.2fs", elapsed * (1.0/CLOCKS_PER_SEC));
- info.push_back(buf);
- }
- if (expected == received) {
- verdict = "PASSED";
- } else {
- verdict = "FAILED";
- }
- cerr << verdict;
- if (!info.empty()) {
- cerr << " (";
- for (int i=0; i<(int)info.size(); ++i) {
- if (i > 0) cerr << ", ";
- cerr << info[i];
- }
- cerr << ")";
- }
- cerr << endl;
- if (verdict == "FAILED") {
- cerr << " Expected: " << expected << endl;
- cerr << " Received: " << received << endl;
- }
- return verdict == "PASSED";
- }
- int run_test_case(int casenum) {
- switch (casenum) {
- case 0: {
- string words[] = {"topcoder"};
- int expected__ = 9;
- clock_t start__ = clock();
- int received__ = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
- return verify_case(casenum, expected__, received__, clock()-start__);
- }
- case 1: {
- string words[] = {"topcoder","topcoder"};
- int expected__ = 9;
- clock_t start__ = clock();
- int received__ = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
- return verify_case(casenum, expected__, received__, clock()-start__);
- }
- case 2: {
- string words[] = {"aab","ca"};
- int expected__ = 5;
- clock_t start__ = clock();
- int received__ = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
- return verify_case(casenum, expected__, received__, clock()-start__);
- }
- case 3: {
- string words[] = {"aab","ca","ba"};
- int expected__ = 5;
- clock_t start__ = clock();
- int received__ = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
- return verify_case(casenum, expected__, received__, clock()-start__);
- }
- case 4: {
- string words[] = {"ab","cd","ef"};
- int expected__ = 7;
- clock_t start__ = clock();
- int received__ = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
- return verify_case(casenum, expected__, received__, clock()-start__);
- }
- case 5: {
- string words[] = {"a","aa","aaa"};
- int expected__ = 4;
- clock_t start__ = clock();
- int received__ = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
- return verify_case(casenum, expected__, received__, clock()-start__);
- }
- // custom cases
- /* case 6: {
- string words[] = ;
- int expected__ = ;
- clock_t start__ = clock();
- int received__ = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
- return verify_case(casenum, expected__, received__, clock()-start__);
- }*/
- /* case 7: {
- string words[] = ;
- int expected__ = ;
- clock_t start__ = clock();
- int received__ = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
- return verify_case(casenum, expected__, received__, clock()-start__);
- }*/
- /* case 8: {
- string words[] = ;
- int expected__ = ;
- clock_t start__ = clock();
- int received__ = PrefixTree().getNumNodes(vector <string>(words, words + (sizeof words / sizeof words[0])));
- return verify_case(casenum, expected__, received__, clock()-start__);
- }*/
- default:
- return -1;
- }
- }
- }
- int main(int argc, char *argv[]) {
- if (argc == 1) {
- moj_harness::run_test();
- } else {
- for (int i=1; i<argc; ++i)
- moj_harness::run_test(atoi(argv[i]));
- }
- }
- // }}} END CUT HERE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement