• 模糊匹配算法


    项目中需要对车牌号码进行模糊匹配,比如5位数相同就认为车牌号码匹配成功。

    参考:

    字符串模糊匹配使用递归实现 - CSDN博客  http://blog.csdn.net/damenggege123/article/details/8213500

    字符串模糊匹配递归实现优化1 - CSDN博客  http://blog.csdn.net/damenggege123/article/details/8213919

    实际上我的实现就是将上面博客中的java代码改成了c++代码而已,所以非常感谢上面的博主!

    下面是修改后的C++实现:

    .h文件:

    #include <string>
    using namespace std;

    class CMatch
    {
    public:
    CMatch();
    ~CMatch();

    public:

    /**
    * 百分之多少之内匹配错误可以接受
    * a与ab匹配为百分之50的错误率。
    * @param percent 设置匹配百分比
    * @param src 字符串1
    * @param dest 字符串2
    * @param hander 匹配规则
    * @return
    */
    bool Match(double percent,string src,string dest);

    /**
    * 几个错误的字符可以接受
    * a与ab为1个字符错误可以接受
    * @param percent 设置匹配百分比
    * @param src 字符串1
    * @param dest 字符串2
    * @param hander 匹配规则
    * @return
    */
    bool Match(int errorNum, string src,string dest);

    private:
    int CalcMatchNumber(string src, int i, string dest, int j, int curdeep,int maxdeep);

    };

    .cpp文件

    #include "Match.h"
    #include <math.h>


    CMatch::CMatch()
    {

    }


    CMatch::~CMatch()
    {

    }

    bool CMatch::Match( double percent,string src,string dest )
    {

    int score = 0;
    int max = src.size() > dest.size() ? src.size(): dest.size();
    score = CalcMatchNumber(src, 0, dest, 0, 0, (int)ceil((1-percent)*max) );

    return score/max > percent;

    }

    bool CMatch::Match( int errorNum, string src,string dest )
    {


    int score = CalcMatchNumber(src, 0, dest, 0, 0, errorNum );
    int max = src.size() > dest.size() ? src.size(): dest.size();
    return max - score <= errorNum;

    }

    int CMatch::CalcMatchNumber( string src, int i, string dest, int j, int curdeep,int maxdeep )
    {

    int score = 0;
    if( curdeep > maxdeep ||i >= src.size() || j >= dest.size())
    return 0;
    //bool ismatch = hander.compare(csrc[i], cdest[j]);

    if( src[i] == dest[j] )
    {
    score++;
    if(i+1<src.size() && j+1<dest.size())
    {
    score += CalcMatchNumber(src, i+1, dest, j+1, 0,maxdeep);
    }

    }
    else
    {
    int temp1 = 0;
    int temp2 = 0;
    int temp3 = 0;
    temp1 += CalcMatchNumber(src, i, dest, j+1, curdeep+1,maxdeep) ;
    temp2 += CalcMatchNumber(src, i+1, dest, j, curdeep+1,maxdeep) ;
    temp3 += CalcMatchNumber(src, i+1, dest, j+1, curdeep+1,maxdeep) ;
    int temp4 = std::max(temp1, temp2);
    score += std::max(temp3, temp4);
    }
    return score;

    }

    使用示例:

    string str1 = "辽A56633";
    string str2 = "辽A56635";
    string str3 = "吉A54633";


    CMatch matcher;
    bool bRet = matcher.Match(5, str1, str3);

    其他参考资料:

  • 相关阅读:
    FC网络学习笔记02 -网络配置方法 分类: 网络基础 2013-10-10 09:43 765人阅读 评论(0) 收藏
    FC网络学习笔记01 分类: 网络基础 2013-10-10 09:42 865人阅读 评论(0) 收藏
    CPLD/FPGA厂商概述 . 分类: 硬件开发_FPGA 2013-10-09 16:54 539人阅读 评论(0) 收藏
    @property用法总结
    python的目录
    su
    os.path.join路径拼接
    python的类变量和对象变量[转]
    python的mutable变量与immutable变量
    dataframe操作
  • 原文地址:https://www.cnblogs.com/nanzhi/p/8462911.html
Copyright © 2020-2023  润新知