zoukankan      html  css  js  c++  java
  • 串的模式匹配算法之二:首尾匹配算法

    using System;
    namespace EasyIndex
    {
        
    class Index
        
    {
            
    static void Main(string[] args)
            
    {
                
    string s="acabaabcaabaabcac";
                
    string m="abaabcac";
                
    int i=0;
                
    int j=0;
                
    while(i<s.Length && j<m.Length)
                
    {
                    
    if(s[i]==m[0])//判断两串的第一个元素是否相等
                    {
                        
    if(s[i+m.Length-1]==m[m.Length-1])//如果头元素相等,再判断尾元素是否相等;
                        {
                            i
    =1;
                            j
    =0;
                            
    while(i<s.Length && j<m.Length)//如果头尾都相等,再从第二个元素到倒数第二个元素判断;
                            {
                                
    if(s[i]==m[j])
                                
    {
                                    i
    ++;
                                    j
    ++;
                                }

                                
    else
                                
    {
                                    i
    =i-j+1;
                                    j
    =0;
                                }

                            }

                        }

                        
    else
                        
    {
                            i
    =i-j+1;
                            j
    =0;
                        }

                    }

                    
    else
                    
    {
                        i
    =i-j+1;
                        j
    =0;
                    }

                  }

                
    if(j>=m.Length)//说明m是s的一个子串,而且pos是i-m.Lengh+1,因为数组从0开始计数;
                {
                    Console.WriteLine(
    "m与s中第"+Convert.ToString(i-m.Length+1)+"个元素开始的子串相匹配!");
                }

                
    else
                
    {
                    Console.WriteLine(
    "m不是s的子串!");
                }


            }

        }

    }

  • 相关阅读:
    经典假设检验理论记录一二
    阿里云centos7.3安装tomcat8
    PowerDesigner中Name与Code同步的问题
    PowerDesigner中NAME和COMMENT的互相转换
    树形结构的数据库表设计
    Spring mybatis Access denied for user 'root'@'localhost' (using password:YES)
    IOS上架审核问题
    maven自动部署war包到tomcat 问题
    SpringMVC +Hibernate JPA+Spring-data-jpa
    Hibernate4.3.x Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
  • 原文地址:https://www.cnblogs.com/gmq/p/515610.html
Copyright © 2011-2022 走看看