How to use PHP to calculate the distance between strings

Source: Internet
Author: User
This article mainly describes how to use PHP to calculate the distance between strings, has a certain reference value, now share to everyone, the need for friends can refer to

First, summary

A short summary: The best way to solve DP problem is: To analyze the state after the instance + Draw table.

1. What is the best way to solve DP problems?

Analysis of the state after the instance + Draw table

2. What are the benefits of drawing?

Drawing out the table after the programming is not easy to make mistakes, because with reference, you can follow the reference to write code

Second, calculate the distance of the string

Title Description

Levenshtein distance, also known as the editing distance, refers to the minimum number of edit operations required between two strings, converted from one to another. Permission edits include replacing one character with another character, inserting a character, and deleting a character. The algorithm of editing distance is first put forward by Russian scientist Levenshtein, so it is called Levenshtein Distance.

Ex:

String A:ABCDEFG

String B:abcdef

Achieve the goal by adding or deleting the character "G". Both of these scenarios require one operation. Define the number of times required for this operation as a distance of two strings.

Requirements:

Given any two strings, write an algorithm to calculate their editing distance.

Please implement the following interface

/*  function: Calculates the distance of two strings *  input: String A and string b *  output: None *  return: If the distance of the string is calculated successfully, the return 1 */public   static   int Calstringdistance (String CharA, String  Charb)    {        return  0;    }

Input Description:

Enter two strings

Output Description:

Get calculated results

Example 1

Input

Abcdefgabcdef

Output

1

2. Code (wrong code)

<?php/*1, this is a DP of the topic 2, but also is a linear dp3, F (i) (j) How to get F (i) (j) 4, DP is the brush table, here is clearly brushed 2-dimensional table 5, F (i) (j) What is said: The first I and string 1 of the string 2 is the distance of J So eventually all is F (Len (str1)) (Len (STR2)) 6, state transition equation: If the last of the string 1 and the last character of the string 2 is equal, then f (i) (j) =f (i-1) (j-1), unequal, then f (i) (j) =min (F ( I-1) (j), F (i) (j-1)) 7, when you want to Chabudu, you can draw a table directly into Excel based on an instance, not error-prone and clear fast */while ($str 1=trim (fgets)) {$str STDIN (2=trim (    STDIN));    $len 1=strlen ($str 1);    $len 2=strlen ($str 2);    $DP =null;    for ($i =0; $i <= $len 2; $i + +) {$DP []=array_fill (0,intval ($len 1) +1,0);    } for ($i =0; $i <= $len 1; $i + +) {$DP [0][$i]= $i;    } for ($i =0; $i <= $len 2; $i + +) {$DP [$i][0]= $i;            } for ($i =1; $i <= $len 2; $i + +) {//Line for ($j =1; $j <= $len 1; $j + +) {//column//If str1[$i-1] found in str2:0-$j-1,            $str 1_2=substr ($str 1,0, $j);            if (Strpos ($str 1_2, $str 2[$i-1])!==false) {$DP [$i] [$j]= $dp [$i -1][$j-1];            }else{$DP [$i] [$j]=max ($DP [$i] [$j-1], $DP [$i -1][$j]); }}} echo $DP [$len 2][$len 1].    Php_eol; PrinT_r ($DP);}? >

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.