Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. const map<char, string> morseEncoder =
  9. {  
  10.     {'A',   ".-"},  {'B', "-..."},   {'C', "-.-."},  {'D',  "-.."},
  11.     {'E',    "."}{'F', "..-."},   {'G',  "--."},  {'H', "...."},
  12.     {'I',   ".."},  {'J', ".---"},   {'K',  "-.-"},  {'L', ".-.."},
  13.     {'M',   "--"},  {'N',   "-."},   {'O',  "---"},  {'P', ".--."},
  14.     {'Q', "--.-"},  {'R',  ".-."},   {'S',  "..."},  {'T',    "-"},
  15.     {'U',  "..-"}{'V', "...-"},   {'W', ".--"},   {'X', "-..-"},
  16.     {'Y', "-.--"},  {'Z', "--.."}
  17. };
  18.  
  19. vector<string> charMap;
  20. map<string, int> dictionary;
  21. string morseCode;
  22. int64_t results[100000];
  23. int longestWord = 0;
  24. int shortestWord = 99999999;
  25. void PrepareCharMap()
  26. {
  27.     charMap.resize(256);
  28.     for (auto p : morseEncoder)
  29.     {
  30.         charMap[p.first] = p.second;
  31.     }
  32. }
  33.  
  34. int64_t GetWordsCount(int wordStart, int wordSz)
  35. {
  36.     std::string wordCode = morseCode.substr(wordStart, wordSz);
  37.     auto it = dictionary.find(wordCode);
  38.     if (it == dictionary.end())
  39.     {
  40.         return 0;
  41.     }
  42.     return it->second;
  43. }
  44.  
  45. int64_t Calc(int start)
  46. {
  47.     if (start == morseCode.size())
  48.     {
  49.         return 0;
  50.     }
  51.     if (results[start] != -1)
  52.     {
  53.         return results[start];
  54.     }
  55.     int64_t res = 0;
  56.     int endSz;
  57.     if (start + longestWord > morseCode.size())
  58.     {
  59.         endSz = morseCode.size() - start;
  60.     }
  61.     else
  62.     {
  63.         endSz = longestWord;
  64.     }
  65.     for(int sz = shortestWord; sz <= endSz; sz++)
  66.     {
  67.         int64_t wordsCount = GetWordsCount(start, sz);
  68.         if (0 == wordsCount)
  69.         {
  70.             continue;
  71.         }
  72.         int64_t combinationsCount;
  73.         if (start + sz == morseCode.size())
  74.         {
  75.             combinationsCount = 1;
  76.         }
  77.         else
  78.         {
  79.             combinationsCount = Calc(start+sz);
  80.         }
  81.         res += wordsCount * combinationsCount;
  82.     }
  83.     results[start] = res;
  84.     return res;
  85. }
  86.  
  87. int main()
  88. {
  89.     PrepareCharMap();
  90.     for (int i = 0; i < 100000; i++) results[i] = -1;
  91.     int dictionarySz;
  92.  
  93.     getline(cin, morseCode);
  94.     cin >> dictionarySz; cin.ignore();
  95.  
  96.     for (int i = 0; i < dictionarySz; i++)
  97.     {
  98.         string word;
  99.         getline(cin, word);
  100.         string morseWord = "";
  101.         for (char c : word)
  102.         {
  103.             morseWord += charMap[c];
  104.         }
  105.         if (morseWord.size() > longestWord)
  106.         {
  107.             longestWord = morseWord.size();
  108.         }
  109.         if (morseWord.size() < shortestWord)
  110.         {
  111.             shortestWord = morseWord.size();
  112.         }
  113.         auto it = dictionary.find(morseWord);
  114.         if (it == dictionary.end())
  115.         {
  116.             dictionary[morseWord] = 1;
  117.         }
  118.         else
  119.         {
  120.             it->second++;
  121.         }
  122.     }
  123.     int64_t res = Calc(0);
  124.     cout << res << endl;
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement