函数作用就是匹配字符串是否是源字符串相似的字符串
参数,strPrimary 是模板字符串 , strMatches是需要匹配的字符串.
例如: strPrimary字符串是MMAPLFA字样, strMatches字符串是MAP_FAMB035, 需要匹配的字符串和模板字符串有5个字母像匹配,分别是MAP和FA而且这几个字符相对位置也是一样的.函数返回5.
int StringMatches(const std::string& strPrimary, const std::string& strMatches)
{
int max = 0, num, i, j;
for (int k = strPrimary.size() - 1; k > 0; k--)
{
num = 0;
i = k;
j = 0;
while (1)
{
if (i >= strPrimary.size() || j >= strMatches.size())
{
break;
}
if (strPrimary.at(i++) == strMatches.at(j++))
{
num++;
}
}
max = (num > max) ? num : max;
}
for (int k = 0; k < strMatches.size(); k++)
{
num = 0;
i = 0;
j = k;
while (1)
{
if (i >= strPrimary.size() || j >= strMatches.size())
{
break;
}
if (strPrimary.at(i++) == strMatches.at(j++))
{
num++;
}
}
max = (num > max) ? num : max;
}
return max;
}
优化了一下,添加了一个参数pos,会返回可替换的位置,
int StringMatches(const std::string& strPrimary, const std::string& strMatches, int& pos)
{
int max = 0, num, i, j;
std::map<int, int> mapnum, mapnum1;
for (int k = strPrimary.size() - 1; k > 0; k--)
{
std::map<int, int> tempmap;
for (num = 0, i = k, j = 0; !(i >= strPrimary.size() || j >= strMatches.size()); i++, j++)
{
if (strPrimary.at(i) == strMatches.at(j))
{
tempmap[i] = j;
num++;
}
}
if (num > max)
{
max = num;
mapnum = std::move(tempmap);
}
}
for (int k = 0; k < strMatches.size(); k++)
{
std::map<int, int> tempmap;
for (num = 0, i = 0, j = k; !(i >= strPrimary.size() || j >= strMatches.size()); i++, j++)
{
if (strPrimary.at(i) == strMatches.at(j))
{
tempmap[i] = j;
num++;
}
}
if (num > max)
{
max = num;
mapnum1 = std::move(tempmap);
}
}
if (mapnum.size() >= mapnum1.size())
{
if (mapnum.size() > 0)
{
pos = std::prev(mapnum.end())->second + strPrimary.size() - std::prev(mapnum.end())->first;
}
}
else
{
if (mapnum1.size() > 0)
{
pos = std::prev(mapnum1.end())->second + strPrimary.size() - std::prev(mapnum1.end())->first;
}
}
return max;
}
这是一个csv测试用例第一列是编号,用来打印的
第二列是模板,第三列是需要匹配的字符串,
第四列是可以匹配的字符个数
第五列是可替换的位置
举个例子
0,MMAPLFA,MAP_FAMB035,5,6
MAP_FAMB035这个和模板字符串MMAPLFA比较, M A P F A 这几个字符可以匹配到,不光字符能匹配到,相对位置也没有错,所有第四列是5,而 MAP_FAMB035这个字符的前六个和模板字符匹配,所有是6.
0,MMAPLFA,MAP_FAMB035,5,6
1,MMAPLFA,AP_FAMB035,4,5
2,MMAPLFA,P_FAMB035,3,4
3,MMAPLFA,MMAPLFAMB035,7,7
4,MMAPLFA,MMAP__AMB035,5,7
5,MMAPLFA,MA__FAMB035,4,6
6,MMAPLFA,PLFAMB035,4,4
7,MMAPLFA,MMAP_FAMB035,6,7
8,MMAPLFA,MMAP_F_MB035,5,7
9,MMAPLFA,PLFAMB035,4,4
10,MMAPLFA,MMAP___MB035,4,7
11,MMAPLFA,APLF_MB035,4,5
12,MMAPLFA,MMAPLF_MB035,6,7
使用gtest测试结果
---------------2024.02.28 更新使用lua实现--------------
--strPrimary参数是模板 strMatches是需要匹配的字符串
function StringMatches(strPrimary, strMatches)
local table = {};
local table1 = {};
local maxValue = 0;
local i = 1;
local j = 1;
local num = 0;
local pos = 0;
local k = string.len(strPrimary);
while k > 0 do
i = k;
j = 1;
num = 0;
local tempTable = {};
while not (i > string.len(strPrimary) or j > string.len(strMatches)) do
if strPrimary:sub(i, i) == strMatches:sub(j, j) then
tempTable[i] = j;
num = num + 1;
end
i = i + 1;
j = j + 1;
end
if num > maxValue then
maxValue = num;
table = tempTable;
end
k = k - 1;
end
local tableLenth = 0;
for a in pairs(table) do
tableLenth = tableLenth + 1;
end
k = 1;
while k <= string.len(strMatches) do
num = 0;
i = 1;
j = k;
local tempTable = {};
while not (i > string.len(strPrimary) or j > string.len(strMatches)) do
if strPrimary:sub(i, i) == strMatches:sub(j, j) then
tempTable[i] = j;
num = num + 1;
end
i = i + 1;
j = j + 1;
end
if num > maxValue then
maxValue = num;
table1 = tempTable;
end
k = k + 1;
end
local table1Lenth = 0;
for a in pairs(table1) do
table1Lenth = table1Lenth + 1;
end
if tableLenth >= table1Lenth then
if tableLenth ~= 0 then
local lastkey = 0;
local lastvalue = 0;
for key, value in pairs(table) do
if key > lastkey then
lastkey = key;
lastvalue = value;
end
end
pos = lastvalue + #strPrimary - lastkey;
end
else
if tableLenth ~= 0 then
local lastkey = 0;
local lastvalue = 0;
for key, value in pairs(table1) do
if key > lastkey then
lastkey = key;
lastvalue = value;
end
end
pos = lastvalue + #strPrimary - lastkey;
end
end
return maxValue, pos;
end