zoukankan      html  css  js  c++  java
  • 【算法】拼音匹配算法

    之前一直想做个通讯录程序,和音乐快速搜索程序,需要使用到汉字注音,还有字符串快速匹配。

    可一直没有找到高效率的算法,普通的字符串匹配算法速度太慢,如何才能在最短的时间内匹配出需要的字符串,这个问题困惑了几个星期,今天把字符串匹配算法共享出来,具体效果跟QQ通讯录查找联系人的时候差不多

    测试:

      测试字符串:        string[] pinyin = {"chen", "neng", "guang"}

      测试Key:    "chng"  "chnengguang"  "cheng"  "chenengua"

    基本思想:

      从Key的第一个字符开始,在字符串数组的第一个pinyin[0] 开始匹配

        1、当遇到追尾(字符串尾) 或 不匹配时

          跳到下一个拼音(如:pinyin[1])开始匹配

          1、不匹配时:

            使测试Key回退一个字符再进行匹配,匹配不完成,则继续回退,若回到第一个字符还是不匹配,则返回false

          2、匹配时:

            继续往下匹配

        2、继续匹配

    图解: 

    代码(C#)

            public static bool PinyinMatch(string search, string[] pinyin)
            {
                int wordIndex = 0;
                int wordStart = 0;
                int searchIndex = 0;
    
                int pinyinLen = pinyin.Length;
                while (searchIndex < search.Length && wordIndex < pinyinLen)
                {
                    //不追尾,判断是否匹配
                    if (wordStart < pinyin[wordIndex].Length && search[searchIndex] == pinyin[wordIndex][wordStart])
                    {
                        searchIndex++;
                        wordStart++;
                    }
                    //追尾或匹配失败
                    else
                    {
                        //到最后一个拼音,无法匹配
                        if (wordIndex == pinyinLen - 1)
                        {
                            return false;
                        }
                        wordIndex++;
                        wordStart = 0;
                        //判断是否匹配
                        if (search[searchIndex] == pinyin[wordIndex][wordStart])
                        {
                            searchIndex++;
                            wordStart++;
                        }
                        //不匹配,回退
                            //补充(未完成):回退到最左边一个字符,pinyins可以向后跳一位
                        else
                        {
                            if (searchIndex > 0)
                            {
                                searchIndex--;
                                //判断是否匹配
                                while (searchIndex >= 0 && search[searchIndex] != pinyin[wordIndex][0])
                                {
                                    searchIndex--;
                                }
                                if (searchIndex < 0)
                                {
                                    searchIndex = 0;
                                    wordIndex++;
                                    wordStart = 0;
                                }
                                else
                                {
                                    searchIndex++;
                                    wordStart++;
                                }
                            }
                        }
                    }
                }
                if (searchIndex == search.Length)
                {
                    return true;
                }
                return false;
            }

        

      

  • 相关阅读:
    HTML5触摸屏touch事件使用介绍1
    hdu 1408 acdearm &quot;Money, Money, Money&quot;
    基于QTP的自己主动化測试框架介绍
    ExpandListView onChildClickListener 失效
    hdu1227 Fast Food
    Linux C高级编程——文件操作之系统调用
    nodejs之路-[0]安装及简易配置
    动态加入改动删除html表格内容
    socket网络编程基础小记
    LeetCode OJ Minimum Depth of Binary Tree 递归求解
  • 原文地址:https://www.cnblogs.com/bomo/p/2798229.html
Copyright © 2011-2022 走看看