Back method/KMP algorithm-Find string

Source: Internet
Author: User

    1. Back to the law: in the string search is most likely to think of a violent search, that is, the return method. The idea is to look for each character of the string to be taken out, and then in order to find in the source string, if found to return true, otherwise the source string index to move one bit backward, and then repeatedly find, until found to return true, or the source string is not found to find the return false; Again, because each lookup fails, the source string needs to go back to the beginning of the next position to start looking, so the algorithm is also called the back method.
    2. KMP algorithm: First parse the string to find and find the duplicate substring. The target string is then compared to the source string, and once the comparison fails, it is not returned, but is transferred directly to the next position of the repeating substring, depending on the repetition of the target string string. This reduces the number of back-up, the complexity of the algorithm is very much reduced.

The following code shows the back and KMP algorithms and tests them.

 PackageOrg.lyk.main;Importorg.lyk.entities.List; Public classmain{ Public Static voidMain (string[] args) {String _source= "Hello How is You"; String _target= "Ello how Yo";        System.out.println (_source);                System.out.println (_target);        System.out.println (STRCONTAIN_BF (_source, _target));    System.out.println (STRCONTAINS_KMP (_source, _target)); }           Public Static BooleanSTRCONTAINS_KMP (String _source, String _target) {BooleanRetVal =false; if(NULL= = _source | |NULL= = _target | | ". Equals (_source) | |" ". Equals (_target))return false; Char[] Source =_source.tochararray (); Char[] target =_target.tochararray (); intSourcelength =source.length; intTargetlength =target.length; if(Targetlength >sourcelength)return false; Else if(Targetlength = =sourcelength) {             for(inti = 0; i < sourcelength; i++)            {                if(Source[i]! =Target[i])return false; }            return true; }         Else        {            int[] Next =GetNext (target); intCurrentsourceindex = 0; intCurrenttargetindex = 0;  while(Currenttargetindex <targetlength) {                intK = 0; BooleanFlag =true;  for(inti = Currenttargetindex; i<targetlength; i++)                {                     if((Currentsourceindex + k) >=sourcelength) {                         return false; }                                        if(Target[i] = = source[currentsourceindex+K]) {k++; Continue; }                    Else{flag=false; if(i = = 0) {Currentsourceindex++; }                        Else{Currentsourceindex= Currentsourceindex +K; } Currenttargetindex=Next[currenttargetindex];  Break; }                }                if(Flag = =true)                    return true; }            return true; }     }             Public Static int[] GetNext (Char[] target) {        intTargetlength =target.length; int[] Next =New int[Targetlength]; if(Targetlength <= 2)        {             for(inti = 0; i< targetlength; i++) {Next[i]= 0; }            returnNext; }        Else{next[0] = 0; next[1] = 0;  for(inti = 2; i<targetlength; i++)            {                intCount = 0;  for(intj = 0; J < i-1;j++)                {                    BooleanFlag =true;  for(intk = 0; K <=j; k++)                    {                        if(Target[k]! = target[i-1-j+K]) flag=false; }                    if(Flag = =true) Count= J+1; } Next[i]=count; }            returnNext; }    }         Public Static BooleanSTRCONTAIN_BF (String _source,string _target) {BooleanRetVal =false; if(NULL= = _source | |NULL= = _target | | ". Equals (_source) | |" ". Equals (_target))return false; Char[] Source =_source.tochararray (); Char[] target =_target.tochararray (); intSourcelength =source.length; intTargetlength =target.length; if(Targetlength >sourcelength)return false; Else if(Targetlength = =sourcelength) {             for(inti = 0; i < sourcelength; i++)            {                if(Source[i]! =Target[i])return false; }            return true; }         Else        {             for(inti = 0; I <= sourcelength-targetlength;i++)            {                BooleanFlag =true;  for(intj = 0; J < Targetlength; J + +)                {                    if(Target[j]! = source[i+J]) {flag=false;  Break; }                }                if(Flag = =true)                    return true; }            return false; }    }}

Back method/KMP algorithm-Find string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.