Similar String algorithm

后端 未结 9 1385
梦如初夏
梦如初夏 2020-11-29 20:10

I\'m looking for an algorithm, or at least theory of operation on how you would find similar text in two or more different strings...

Much like the question posed he

9条回答
  •  温柔的废话
    2020-11-29 20:23

    i had a similar problem, i needed to get the percentage of characters in a string that were similar. it needed exact sequences, so for example "hello sir" and "sir hello" when compared needed to give me five characters that are the same, in this case they would be the two "hello"'s. it would then take the length of the longest of the two strings and give me a percentage of how similar they were. this is the code that i came up with

    int compare(string a, string b){
       return(a.size() > b.size() ? bigger(a,b) : bigger(b,a));
    }
    
    
    
    int bigger(string a, string b){
    
    
    
    int maxcount = 0, currentcount = 0;//used to see which set of concurrent characters were biggest
    
    for(int i = 0; i < a.size(); ++i){
    
        for(int j = 0; j < b.size(); ++j){
    
            if(a[i+j] == b[j]){
    
             ++currentcount;
    
             }
    
            else{
    
                if(currentcount > maxcount){
    
                 maxcount = currentcount;
    
                 }//end if
    
                 currentcount = 0;
    
                }//end else
    
            }//end inner for loop
    
        }//end outer for loop
    
    
       return ((int)(((float)maxcount/((float)a.size()))*100));
    }
    

提交回复
热议问题