init: init commit
All checks were successful
Build and Deploy Quartz / build-and-deploy (push) Has been skipped

This commit is contained in:
2026-01-01 14:16:34 +08:00
commit 4a5acab07d
216 changed files with 40383 additions and 0 deletions

394
test.cpp Normal file
View File

@@ -0,0 +1,394 @@
#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;
// 123123
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;
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<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()
{
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<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();
ansCache.showCacheStatus();
return found;
}
};