396 lines
7.9 KiB
C++
396 lines
7.9 KiB
C++
#include <algorithm>
|
|
#include <format>
|
|
#include <iostream>
|
|
#include <numeric>
|
|
#include <string>
|
|
#include <unordered_set>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
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<char>()(top) + std::hash<char>()(left) +
|
|
std::hash<char>()(right);
|
|
}
|
|
};
|
|
|
|
struct PatternHash
|
|
{
|
|
size_t operator()(const Pattern &p) const { return p.hash(); }
|
|
};
|
|
|
|
class PatternManager
|
|
{
|
|
private:
|
|
std::unordered_set<Pattern, PatternHash> 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<string, bool> mAllowedBottomcache;
|
|
|
|
string wholeRowToString(unsigned rowIdx)
|
|
{
|
|
string s = "";
|
|
for (int i = 0; i <= rowIdx; ++i)
|
|
{
|
|
s.push_back(pyramid[rowIdx][i]);
|
|
}
|
|
|
|
return s;
|
|
}
|
|
|
|
public:
|
|
AnswerCache() {}
|
|
|
|
// using the curR and curC, this method will work out the
|
|
// bottom string of the sub-pyramid that use such index as top element
|
|
// then set the cache for that bottom sequence
|
|
void setCache(bool state, bool onlySetWhenNotExists = false)
|
|
{
|
|
// deal with each layer from curR to the bottom
|
|
for (unsigned workingRow = curR + 1; workingRow < bottomSize; ++workingRow)
|
|
{
|
|
// figure out the target string that need to be cache for this layer
|
|
string workingStr = wholeRowToString(workingRow);
|
|
string subStr = workingStr.substr(curR, workingRow - curR + 1);
|
|
|
|
if (onlySetWhenNotExists && mAllowedBottomcache.contains(subStr))
|
|
{
|
|
return;
|
|
}
|
|
mAllowedBottomcache[subStr] = state;
|
|
}
|
|
}
|
|
|
|
// 0: false, 1: true, -1:not cached
|
|
int getCache(int rowIdx)
|
|
{
|
|
string rowStr = wholeRowToString(rowIdx);
|
|
if (mAllowedBottomcache.contains(rowStr))
|
|
{
|
|
return mAllowedBottomcache[rowStr];
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
void clear()
|
|
{
|
|
mAllowedBottomcache.clear();
|
|
}
|
|
};
|
|
|
|
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();
|
|
ansCache.clear();
|
|
|
|
// clear answer cache
|
|
|
|
// 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<string> &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()
|
|
{
|
|
// search end
|
|
if (curChar() == '1')
|
|
{
|
|
return;
|
|
}
|
|
|
|
// search not begin
|
|
if (curChar() == '0')
|
|
{
|
|
curChar() = 'A';
|
|
if (curValid())
|
|
{
|
|
ansCache.setCache(true);
|
|
return;
|
|
}
|
|
}
|
|
|
|
++curChar();
|
|
|
|
while (curChar() <= 'F' && !curValid())
|
|
{
|
|
++curChar();
|
|
}
|
|
|
|
if (curChar() > 'F')
|
|
{
|
|
curChar() = '1';
|
|
ansCache.setCache(false, true);
|
|
}
|
|
else
|
|
{
|
|
ansCache.setCache(true);
|
|
}
|
|
}
|
|
|
|
void _search()
|
|
{
|
|
|
|
// already found answer
|
|
if (found || finished)
|
|
{
|
|
return;
|
|
}
|
|
|
|
setToNextPossibleChar();
|
|
|
|
bool isDelta;
|
|
|
|
// this position finished
|
|
if (curChar() == '1')
|
|
{
|
|
isDelta = deltaSearchpointer(0);
|
|
if (!isDelta)
|
|
{
|
|
finished = 1;
|
|
}
|
|
return;
|
|
}
|
|
|
|
// this position not finish searching
|
|
|
|
// try use cache for next layer if this position is a start of new layer
|
|
if (curC == 0 && curChar() == '0')
|
|
{
|
|
bool cacheRes = ansCache.getCache(curR + 1);
|
|
if (cacheRes == 1)
|
|
{
|
|
found = true;
|
|
return;
|
|
}
|
|
else if (cacheRes == 0)
|
|
{
|
|
deltaSearchpointer(0);
|
|
return;
|
|
}
|
|
}
|
|
|
|
isDelta = deltaSearchpointer(1);
|
|
if (!isDelta)
|
|
{
|
|
found = 1;
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
curChar() = '0';
|
|
}
|
|
}
|
|
|
|
public:
|
|
void search()
|
|
{
|
|
while (!found && !finished)
|
|
{
|
|
_search();
|
|
}
|
|
}
|
|
};
|
|
|
|
class Solution
|
|
{
|
|
public:
|
|
bool pyramidTransition(string bottom, vector<string> &allowed)
|
|
{
|
|
// initialization
|
|
bottomSize = bottom.size();
|
|
auto initer = Initializer();
|
|
initer.initVariables();
|
|
initer.initPyramidBottom(bottom);
|
|
initer.initPattern(allowed);
|
|
|
|
// search the answer
|
|
auto searcher = Searcher();
|
|
searcher.search();
|
|
return found;
|
|
}
|
|
}; |