#include #include #include #include #include #include #include #include using std::cout, std::endl; using std::format; using std::vector, std::string; bool debug = false; /** * Data structure to store pattern in question * * Init: * - Use 3-char string to init (based on questoni definition) * - Use three different char parameter to init * * Method: * - Equal comparator * - hash() function */ struct Pattern { char top; char left; char right; Pattern(const string &data) { top = data[2]; left = data[0]; right = data[1]; } Pattern(char top, char left, char right) : top(top), left(left), right(right) {} bool operator==(const Pattern &other) const { return top == other.top && left == other.left && right == other.right; } size_t hash() const { return std::hash()(top) + std::hash()(left) + std::hash()(right); } }; struct PatternHash { size_t operator()(const Pattern &p) const { return p.hash(); } }; class PatternManager { private: std::unordered_set mPatterns; bool mPairs[6][6]; public: PatternManager() { clear(); } void add(const Pattern &p) { mPatterns.insert(p); mPairs[p.left - 'A'][p.right - 'A'] = 1; } bool exists(const Pattern &p) { if (!mPairs[p.left - 'A'][p.right - 'A']) { return false; } return mPatterns.find(p) != mPatterns.end(); } void clear() { mPatterns.clear(); for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { mPairs[i][j] = 0; } } } }; // global variables // statistics unsigned bottomSize = 0; string bottomString; char pyramid[6][6]; // 0: not searched, A-F: searching, 1: finished PatternManager pMgr; // search temp unsigned curR, curC; bool found, finished; class AnswerCache { private: std::unordered_map mAllowedBottomcache; unsigned long long cacheHit, cacheWrite, cacheRead; private: string wholeRowToString(unsigned rowIdx) { string s = ""; for (int i = 0; i <= rowIdx; ++i) { s.push_back(pyramid[rowIdx][i]); } return s; } public: AnswerCache() {} void setCache() { mAllowedBottomcache[wholeRowToString(curR)] = false; } // false: not in cache, true: hit int getCache(int rowIdx) { ++cacheRead; string rowStr = wholeRowToString(rowIdx); return mAllowedBottomcache.contains(rowStr); } void showCacheStatus() { cout << format("cache R/W: {}/{}, cache hit:{}\n", cacheRead, cacheWrite, cacheHit); } void clear() { mAllowedBottomcache.clear(); cacheHit = 0; cacheRead = 0; cacheWrite = 0; } }; AnswerCache ansCache; class Initializer { public: // init variables and clear mMgr void initVariables() { // init temp curR = bottomSize - 2; curC = bottomSize - 2; found = 0; finished = 0; // clear manager pMgr.clear(); // clear answer cache ansCache.clear(); // init pyramid for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { pyramid[i][j] = '0'; } } } // init bottomString and the pyramid array void initPyramidBottom(const string &data) { bottomString = data; for (int i = 0; i < bottomSize; ++i) { pyramid[bottomSize - 1][i] = data[i]; } } // fill data to mMgr void initPattern(const vector &allowed) { for (const auto &s : allowed) { pMgr.add(Pattern(s)); } } }; class Searcher { private: bool deltaSearchpointer(bool forward) { // forward if (forward) { if (curR <= 0 && curC <= 0) { return false; } if (curC > 0) { --curC; } else { --curR; curC = curR; } return true; } // backward else { if (curC >= bottomSize - 2 && curR >= bottomSize - 2) { return false; } if (curC < curR) { ++curC; } else { ++curR; curC = 0; } return true; } } char &curChar() { return pyramid[curR][curC]; } char &leftChar() { return pyramid[curR + 1][curC]; } char &rightChar() { return pyramid[curR + 1][curC + 1]; } bool atTop() { return curR == curC && curC == 0; } bool curValid() { return pMgr.exists(Pattern(curChar(), leftChar(), rightChar())); } void setToNextPossibleChar() { bool firstMove = (curChar() == '0'); // search end if (curChar() == '1') { return; } // search not begin if (curChar() == '0') { curChar() = 'A'; if (curValid()) { return; } } ++curChar(); while (curChar() <= 'F' && !curValid()) { ++curChar(); } if (curChar() > 'F') { curChar() = '1'; } } void _search() { if (debug) { cout << "enter _search()" << endl; } // already found answer if (found || finished) { cout << "found or finished" << endl; return; } // try use cache to check if we could skip current layer // before we are about to start a new upper layer if (curC == curR && curChar() == '0') { if (ansCache.getCache(curR + 1)) { deltaSearchpointer(0); return; } } setToNextPossibleChar(); bool isDelta; // this position finished if (curChar() == '1') { isDelta = deltaSearchpointer(0); // finished if (!isDelta) { finished = 1; } // back to lower layer if (curC == 0) { // remember this layer of pattern doesn't work ansCache.setCache(); } return; } // this position not finish searching isDelta = deltaSearchpointer(1); if (!isDelta) { found = 1; } else { curChar() = '0'; } return; } public: void search() { while (!found && !finished) { _search(); if (debug) { cout << format("found={}, finished={}\n", found, finished); } } } }; class Solution { public: bool pyramidTransition(string bottom, vector &allowed) { // initialization bottomSize = bottom.size(); auto initer = Initializer(); initer.initVariables(); initer.initPyramidBottom(bottom); initer.initPattern(allowed); // search the answer auto searcher = Searcher(); searcher.search(); ansCache.showCacheStatus(); return found; } };