Subchains: Sliding window dictionary string matching

Consider the following problem. They give us a set of patterns (chains) $$Pi = { pi_i }$$, a text $$s$$and a window length $$k$$. We want a list of all shifts $$0 le i le | s | -k$$ such that each pattern in $$Pi$$ is contained in the substring $$s (i: i + k)$$.

Can this be resolved in linear or near linear time? Of course, it can be solved in quadratic time $$O (| s | | Pi | + sum | pi_i |)$$ using KMP or Aho-Corasick plus postprocessing.

The motivation for this problem is to find matches for a topic (represented by the set of patterns) in a text. In that context, it makes sense to demand that the coincidences do not overlap, so I am also interested in that case, but it might be easier to start with the relaxed version.

I would also be interested in the generalizations of the problem that allow approximate matches of some kind, for example, that only require a threshold in the size of the subset of matching patterns, that allow matches within a given editing distance, or something that uses Markov models hidden or general Probabilistic graphic models. However, I would be surprised if such generalization can be resolved in a sub-quadratic time.

sql server: function to obtain the current or default value of a two-value string

I have SQL scripts that I want to be directly executable in the editor and also in automated integration tests. However, in each use case they require different environment and version parameters. I can not use string_split because we use an old SQL Server 2012.

To differentiate between them, I created the GetCurrentOrDefault function that can use the first or second value.

I start by finding the index of the | What do I use as a separator? Then I get the string before and after this character. Finally, I check if the string has the format '{%}|%' and if so, then I use the default version, otherwise the current version. (All magic numbers are arbitrary cousins).

if object_id('dbo.GetCurrentOrDefault') is not null drop function GetCurrentOrDefault

go

create function GetCurrentOrDefault(@valueOrDefault nvarchar(59)) returns nvarchar(17)
begin
declare @current_value as nvarchar(17);
declare @default_value as nvarchar(17);
declare @value_separator_index as int

select @value_separator_index = charindex('|', @valueOrDefault)

if @value_separator_index = 0 return null

select @current_value = substring(@valueOrDefault, 0, @value_separator_index)
select @default_value = substring(@valueOrDefault, @value_separator_index + 1, len(@valueOrDefault) - @value_separator_index + 1)
return iif(@valueOrDefault like '{%}|%', @default_value, @current_value)
end

go

Example of use:

declare @env as nvarchar(51) = N'{Environment}|production'
declare @ver as nvarchar(59) = N'{Version}|3.9.0'

select @env = dbo.GetCurrentOrDefault(@env)
select @ver = dbo.GetCurrentOrDefault(@ver)

if @env is null raiserror ('Invalid environment: ' + @env, 16, 1)
if @ver is null raiserror ('Invalid version: ' + @ver, 16, 1)

Tests replace placeholders with their custom values ​​such as:

.GetSql().Format(new { Environment = "test", Version = "4.0.0" })

What you think? Is there a smarter solution?

javascript – Object in string format within the value of a JSON

Well I have the following JSON:

{
"creatorId": "#1",
"data": {
"id": "10",
"creator": "#1"
},
"subs": ("1")
}

But data content should not be an object. But an object in string format.

{
"creatorId": "#1",
"data": "{
"id": "10",
"creator": "#1"
}",
"subs": ("1")
}

All this is because my intention is to collect the JSON data field and convert it into a js object.

const dataObj = JSON.parse(recoverJson.data);

And of course the simplest thing would be to pass the object directly and not have to be picking up the string and so on. But it is an API requirement that what comes is a string.

How could I mount the JSON with a "strigified" object

Obtain the key and its value from a string "Key: Value; Key: Value; Key: Value" in C ++

I provide a method as follows, what about your ideas? Would you share your ideas with me? Would you give me some recommendations in my code?

live demo in coliru

#include
#include
#include
#include
using namespace std;

The function is divided, divides a string "Key: Value; Key: Value; Key: Value" into a single "Key: Value" by the sign & # 39 ;; & # 39 ;.

void Split(const string strSign, string& strSour, vector& r)
{
if(strSign.length() > 0 && strSour.length() > 0)
{
std::string::size_type found =  strSour.find(strSign);
if(found!=std::string::npos)
{
string strPart1;
string strPart2;
int nSize = strSour.size();

strPart1 = strSour.substr(0,found);
strPart2 = strSour.substr(found+1,nSize);

r.push_back(strPart1);
if(strPart2.length() > 0)//middle
{
Split(strSign,strPart2,r);
}
}
else if(strSour.c_str())//the last one doesn't have sign
{
r.push_back(strSour);
}
}
}

Divide the string "Key: Value" into the string "Key" and "Value" with the sign & # 39;: & # 39 ;, then insert on the map

void MapKeyValue(const string strSign, string& strSour, map& r)
{
if(strSign.length() > 0 && strSour.length() > 0)
{
std::string::size_type found =  strSour.find(strSign);
if(found!=std::string::npos)
{
string strPart1;
string strPart2;
int nSize = strSour.size();

strPart1 = strSour.substr(0,found);
strPart2 = strSour.substr(found+1,nSize);

if(strPart1.length() > 0 && strPart2.length() >0)//Filter the invalid item
{
if(!r(strPart1).length())//if duplicates appear, select the first one
{
r(strPart1) = strPart2;
}
}
}
}
}

void GetKeyValue(string& strSour, map& r)
{
vector temp;
//1. split  by sign ';'
Split(";",strSour,temp);

//2. split by sign ':', Map Key & Value
for(int i=0; i<(int)temp.size(); i++)
{
MapKeyValue(":",temp(i),r);
}

//3. release
temp.clear();
}

int main()
{
string strFaceNameKey = "FaceName";
string strFontSizeKey = "FontSize";
string strFaceNameValue = "";
int nFontSizeValue = 0;

string strSour = "FaceName:;FontSize:9;FaceName:Tahoma;FontSize:13";
map temp;
GetKeyValue(strSour, temp);

for(auto it=temp.begin(); it!=temp.end(); it++)
{
if(strFaceNameKey == it->first)
{
strFaceNameValue = it->second;
}
else if(strFontSizeKey == it->first)
{
nFontSizeValue = atoi(it->second.c_str());
}
}
temp.clear();

//print
cout << strFaceNameKey<< ":" << strFaceNameValue<< endl;
cout << strFontSizeKey<< ":" << nFontSizeValue<< endl;
}

Face Name: Tahoma

Font size: 9

php: the user's meta value is not repeated even though Var_Dump shows the correct string

I'm going absolutely crazy with this one, I can't find out why I don't receive the content of the chain.

var_dump is generating the correct result perfectly, however, the content of \$ havemeta is empty. I really need some input.

global \$current_user;
get_currentuserinfo(); // wordpress global variable to fetch logged in user info
\$userID = \$current_user->ID; // logged in user's ID
\$havemeta = get_user_meta(\$userID, 'location_select', true); // stores the value of logged in user's meta data for 'location_select'.
var_dump(\$havemeta);

function shopping_location_text() {
if( is_shop() ) {
print '

YOUR ARE CURRENTLY SHOPPING IN ' . \$havemeta . '

'; } }

Algorithm to find repeated patterns in a large string

For optimization purposes, I am trying to analyze a large list of commands from executed programs to find fragments of commands that are executed over and over again. This problem is similar to finding repeated substrings in a string. However, in my case I am not looking for the longer substrings, but rather the smaller substrings that occur very frequently.

For example, let's say that each command is represented by a letter, so a program might look like xabca yabca xabca yabca. If we look for the longest repeated substrings, the best result is xabca yabca. A "better" result would be abca, even though. Although it is shorter, it occurs more frequently in the chain. a It occurs even more often on its own, but it would be considered too short a coincidence. Therefore, an algorithm must be parameterizable by a minimum and maximum fragment length.

Things I've tried so far:

• I played with suffix trees to find the longest repeated substrings that occur at least k times. While that is simple to implement, it does not work well in my use case, because there are also superimposed substrings. Trying to eliminate them was not very successful either. The approach mentioned in this publication gave incorrect or incomplete results (or I misunderstood the approach), and neither seems to be customizable. Suffix trees still seem to me the most promising approach. Maybe someone has an idea of ​​how to include the minimum / maximum lengths of pieces in the search here?
• Another attempt was to use the substring table that is created for the LZW compression algorithm. The problem with this approach is that it does not find repeated fragments that occur early and also creates increasingly longer table entries the further it processes the input (which makes sense for compression, but not in my use case).
• My best solution so far is the brute force approach, that is, build a dictionary of each possible substring and count how often it occurs at the entrance. However, this is slow for large entries and has a large memory consumption.
• Another idea was to look for unique commands that occur more frequently and then somehow inspect the local environments of those commands for repeated patterns. However, I didn't think of a good algorithm here.

What more algorithms are there that could be useful in this scenario? What I am looking for is not necessarily the best combination, but a good heuristic. My input data is quite large, with strings up to 100 MB in length; The sizes of the pieces will generally be in the range of 10 to 50.

javascript – How to encode a saved game / game state as a string?

I am building an HTML game. For this game I want to implement a save function that should work like some existing games:

The user can choose export, this will create a chain, which represents the state of the game (I guess).
This string can be pasted on a import input and will load the saved game.

I thought about storing the state in a JSON, minifying it, chaining it and coding it in Base64.
But I don't think it's a good idea, users could easily cheat in this game by decoding the chain, changing what they want, coding it again and loading this chain.

I already tried to generate files saved in games with this function and decode them in Base64, which didn't work, so I guess they use a different encoding.

How can I code the JSON like this?

javascript – How to fix toDataURL that returns an empty string

I am trying to convert a simple drawing on a canvas into a base64 url, but when I call aDataURL I get an empty string.

The code is below:

var canvas, context, canvasImage;
var cursorPosition = {
x: undefined,
y: undefined
};
var mouse;
var color = "rgb(0, 123, 255)";
var size = 10;

function throttle(ms, fn) {
var lastCallTime;
return function() {
var now = Date.now();
if (!lastCallTime || now - lastCallTime > ms) {
lastCallTime = now;
fn.apply(this, arguments);
}
};
}

function drawCircle(event) {
context.beginPath();
context.arc(cursorPosition.x, cursorPosition.y, size, 0, 2 * Math.PI);
context.closePath();
context.fillStyle = color;
context.fill();
canvasImage = context.getImageData(
0,
0,
window.innerWidth,
window.innerHeight
);
console.log(canvas.toDataURL());
}
socket.emit("ai");
canvas = document.getElementById("number");
canvas.width = 140;
canvas.height = 140;
context = canvas.getContext("2d");
context.fillStyle = "white";
context.fillRect(0, 0, canvas.width, canvas.height);
window.onmousedown = function() {
mouse = "down";
console.log("hi");
};
window.onmouseup = function() {
mouse = "";
};
window.onmousemove = throttle(0.00000000001, function(evt) {
if (mouse == "down") {
var rect = canvas.getBoundingClientRect(), // abs. size of element
scaleX = canvas.width / rect.width, // relationship bitmap vs. element for X
scaleY = canvas.height / rect.height; // relationship bitmap vs. element for Y

cursorPosition = {
x: (evt.clientX - rect.left) * scaleX, // scale mouse coordinates after they have
y: (evt.clientY - rect.top) * scaleY // been adjusted to be relative to element
};

drawCircle(event);
}
});

window.ontouchmove = throttle(10, function(event) {
var rect = canvas.getBoundingClientRect(), // abs. size of element
scaleX = canvas.width / rect.width, // relationship bitmap vs. element for X
scaleY = canvas.height / rect.height; // relationship bitmap vs. element for Y

cursorPosition = {
x: (evt.clientX - rect.left) * scaleX, // scale mouse coordinates after they have
y: (evt.clientY - rect.top) * scaleY // been adjusted to be relative to element
};
console.log(cursorPosition);
drawCircle(event);
});
};

I can draw on the canvas but when I call it on the console or in the code I get an empty string like this: ""

Any help would be greatly appreciated. I have tried many things online, but nothing helps.

r: extract information from a string

I have the following string

I would like to extract only the prices within this chain