• 算法笔记 --- String Rotation


    class Rotation {
     
    public:
        bool chkRotation(string A, int lena, string B, int lenb) {
            if(lena != lenb)
                return false;
            
            string str_target = A + A;
            if(KMP_Find(str_target, B))
                return true;
            else
                return false;
            
        }
        bool KMP_Find(string& s,string& t)  
        {  
             int i=0,j=0;  
             int n =t.size();  
             int *next = new int[n];  
             GetNext(t,next);  
             while(i<s.size()&&j<t.size())  
              {  
                 if((j == 0)||(s.at(i)==t.at(j)))  
                {  
                      i++;  
                    j++;  
                }else{  
                    j = next[j];  
                }  
               }
            if(j==t.size())  
              {  
                   //int index=i-t.size()+1;  
                   //cout<<"子串从长串的第"<<index<<"位开始匹配成功!"<<endl;    
                   return true;  
              }  
             //cout<<"字符串匹配失败!"<<endl; 
              return false;  
        }
        void GetNext(string& s,int *next)  
        {  
            int len = s.size();  
             next[0] = 0;  
             next[1] = 0;  
             int i = 1;  
             while(i < len - 1)  
            {  
                int j = next[i];  
      
                while(j > 0 && s.at(j) != s.at(i)) j = next[j];  
      
                if(s.at(j) == s.at(i))  next[i + 1] = j + 1;  
                else next[i + 1] = j;  
      
                i++;  
              } 
        } 
    };
  • 相关阅读:
    commonjs promise/A 规范
    Java之抽象类
    Java中多态的用法
    Java之方法的重写
    Java中static的用法
    java中this的用法
    Java概述
    二叉排序树的创建、插入、删除
    常用排序算法
    LeetCode小白菜笔记[2]:Reverse Integer
  • 原文地址:https://www.cnblogs.com/zhongzhiqiang/p/5791095.html
Copyright © 2020-2023  润新知