我是靠谱客的博主 机灵小熊猫,最近开发中收集的这篇文章主要介绍两个字符串相似度百分比匹配(支持中文字符串匹配)C++,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#pragma once
#ifndef SIMILARITY_H
#define SIMILARITY_H

#include
#include

std::vectorstd::string getStringArray(std::string& SA)
{
std::vectorstd::string tmp;
for (size_t i = 0; i < SA.size()????
{
if (SA[i] & 0x80)
{
tmp.push_back(SA.substr(i, 2));
i += 2;
}
else
tmp.push_back(SA.substr(i++, 1));
}
return tmp;
}

std::vector<size_t> getNextArray(std::vectorstd::string& sSA)
{
std::vector<size_t> gNA;
if (sSA.size() == 0)
return gNA;
gNA.push_back(0);
if (sSA.size() == 1)
return gNA;
size_t i = 0;
size_t j = 1;
while (i < sSA.size() && j < sSA.size() )
{
if (sSA[i] == sSA[j])
{
gNA.push_back(i + 1);
i++;
j++;
}
else
{
if (i == 0)
{
gNA.push_back(0);
j++;
}
else
{
while (i > 0 && sSA[i] != sSA[j])
{
i = gNA[i - 1];
}
if (sSA[i] == sSA[j])
{
gNA.push_back(i + 1);
i++;
j++;
}
else
{
gNA.push_back(0);
j++;
}
}
}
}
return gNA;
}

int KMP(std::vectorstd::string& mSA, std::string& sString)
{
std::vectorstd::string sSA = getStringArray(sString);
std::vector<size_t> sNA = getNextArray(sSA);
size_t j = 0;
for (size_t i = 0; i < mSA.size(); i++)
{
while (j>0&&mSA[i] != sSA[j])
{
j = sNA[j - 1];
}
if (mSA[i] == sSA[j])
{
j++;
}
if (j == sSA.size())
{
return i + 1 - sSA.size();
}
}
return -1;
}

int SIMILARITY(std::string mString, std::string sString)
{
std::vectorstd::string mSA = getStringArray(mString);
std::vectorstd::string sSA = getStringArray(sString);
size_t maxSubstring = 0;
for (size_t i = 0; i < sSA.size(); i++)
{
std::string tmp= sSA[i];
size_t j = i;
size_t count = 0;
while (KMP(mSA, tmp) != -1)
{
count++;
if (j + 1 == sSA.size())
break;
tmp += sSA[++j];
}
if (maxSubstring < count)
maxSubstring = count;
}
return ((double)maxSubstring / (double)mSA.size())

最后

以上就是机灵小熊猫为你收集整理的两个字符串相似度百分比匹配(支持中文字符串匹配)C++的全部内容,希望文章能够帮你解决两个字符串相似度百分比匹配(支持中文字符串匹配)C++所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部