Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <vector>
- #include <algorithm>
- using namespace std;
- const map<char, string> morseEncoder =
- {
- {'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', "-.--"}, {'Z', "--.."}
- };
- vector<string> charMap;
- map<string, int> dictionary;
- string morseCode;
- int64_t results[100000];
- int longestWord = 0;
- int shortestWord = 99999999;
- void PrepareCharMap()
- {
- charMap.resize(256);
- for (auto p : morseEncoder)
- {
- charMap[p.first] = p.second;
- }
- }
- int64_t GetWordsCount(int wordStart, int wordSz)
- {
- std::string wordCode = morseCode.substr(wordStart, wordSz);
- auto it = dictionary.find(wordCode);
- if (it == dictionary.end())
- {
- return 0;
- }
- return it->second;
- }
- int64_t Calc(int start)
- {
- if (start == morseCode.size())
- {
- return 0;
- }
- if (results[start] != -1)
- {
- return results[start];
- }
- int64_t res = 0;
- int endSz;
- if (start + longestWord > morseCode.size())
- {
- endSz = morseCode.size() - start;
- }
- else
- {
- endSz = longestWord;
- }
- for(int sz = shortestWord; sz <= endSz; sz++)
- {
- int64_t wordsCount = GetWordsCount(start, sz);
- if (0 == wordsCount)
- {
- continue;
- }
- int64_t combinationsCount;
- if (start + sz == morseCode.size())
- {
- combinationsCount = 1;
- }
- else
- {
- combinationsCount = Calc(start+sz);
- }
- res += wordsCount * combinationsCount;
- }
- results[start] = res;
- return res;
- }
- int main()
- {
- PrepareCharMap();
- for (int i = 0; i < 100000; i++) results[i] = -1;
- int dictionarySz;
- getline(cin, morseCode);
- cin >> dictionarySz; cin.ignore();
- for (int i = 0; i < dictionarySz; i++)
- {
- string word;
- getline(cin, word);
- string morseWord = "";
- for (char c : word)
- {
- morseWord += charMap[c];
- }
- if (morseWord.size() > longestWord)
- {
- longestWord = morseWord.size();
- }
- if (morseWord.size() < shortestWord)
- {
- shortestWord = morseWord.size();
- }
- auto it = dictionary.find(morseWord);
- if (it == dictionary.end())
- {
- dictionary[morseWord] = 1;
- }
- else
- {
- it->second++;
- }
- }
- int64_t res = Calc(0);
- cout << res << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement