PHP implements a method example for finding the longest common substring of two strings.

Source: Internet
Author: User

PHP implements a method example for finding the longest common substring of two strings.

This example describes how to evaluate the longest public substring of two strings in PHP. We will share this with you for your reference. The details are as follows:

In the previous article, the PHP Method for Solving the longest public substrings is improved based on java. Here we will look at another public substrings algorithm.

The Code is as follows:

<? Php $ a = 'abceee12345309878 '; $ B = 'hangzhou'; $ c = array (); $ lenht1 = strlen ($ a); $ lenyy = strlen ($ B ); $ startTime = microtime (true); for ($ I = 0; $ I <$ lenht1; $ I ++) {for ($ j = 0; $ j <$ len22; $ j ++) {$ n = ($ I-1> = 0 & $ J-1> = 0 )? $ C [$ I-1] [$ J-1]: 0; $ n = ($ a [$ I] = $ B [$ j])? $ N + 1:0; $ c [$ I] [$ j] = $ n ;}} foreach ($ c as $ key => $ val) {$ max = max ($ val); foreach ($ val as $ key1 = >$ val1) {if ($ val1 = $ max & $ max> 0) {$ cdStr [$ max] = substr ($ B, $ key1-$ max + 1, $ max) ;}} ksort ($ cdStr ); $ endTime = microtime (true); echo "Totle time is ". ($ endTime-$ startTime ). "s ". "<br/>"; print_r (end ($ cdStr); exit;?>

Running result:

Totle time is 0.0012800693512 sabceee

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.