code: simple cache
This commit is contained in:
75
test.cpp
75
test.cpp
@@ -126,38 +126,16 @@ private:
|
|||||||
public:
|
public:
|
||||||
AnswerCache() {}
|
AnswerCache() {}
|
||||||
|
|
||||||
// using the curR and curC, this method will work out the
|
void setCache()
|
||||||
// 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
|
mAllowedBottomcache[wholeRowToString(curR)] = false;
|
||||||
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
|
// false: not in cache, true: hit
|
||||||
int getCache(int rowIdx)
|
int getCache(int rowIdx)
|
||||||
{
|
{
|
||||||
string rowStr = wholeRowToString(rowIdx);
|
string rowStr = wholeRowToString(rowIdx);
|
||||||
if (mAllowedBottomcache.contains(rowStr))
|
return mAllowedBottomcache.contains(rowStr);
|
||||||
{
|
|
||||||
return mAllowedBottomcache[rowStr];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear()
|
void clear()
|
||||||
@@ -289,7 +267,6 @@ private:
|
|||||||
curChar() = 'A';
|
curChar() = 'A';
|
||||||
if (curValid())
|
if (curValid())
|
||||||
{
|
{
|
||||||
ansCache.setCache(true);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -304,11 +281,6 @@ private:
|
|||||||
if (curChar() > 'F')
|
if (curChar() > 'F')
|
||||||
{
|
{
|
||||||
curChar() = '1';
|
curChar() = '1';
|
||||||
ansCache.setCache(false, true);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ansCache.setCache(true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,6 +293,17 @@ private:
|
|||||||
return;
|
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();
|
setToNextPossibleChar();
|
||||||
|
|
||||||
bool isDelta;
|
bool isDelta;
|
||||||
@@ -329,41 +312,33 @@ private:
|
|||||||
if (curChar() == '1')
|
if (curChar() == '1')
|
||||||
{
|
{
|
||||||
isDelta = deltaSearchpointer(0);
|
isDelta = deltaSearchpointer(0);
|
||||||
|
|
||||||
|
// finished
|
||||||
if (!isDelta)
|
if (!isDelta)
|
||||||
{
|
{
|
||||||
finished = 1;
|
finished = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// back to lower layer
|
||||||
|
if (curC == 0)
|
||||||
|
{
|
||||||
|
// remember this layer of pattern doesn't work
|
||||||
|
ansCache.setCache();
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// this position not finish searching
|
// 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);
|
isDelta = deltaSearchpointer(1);
|
||||||
if (!isDelta)
|
if (!isDelta)
|
||||||
{
|
{
|
||||||
found = 1;
|
found = 1;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
curChar() = '0';
|
curChar() = '0';
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
Reference in New Issue
Block a user