Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

朱继来 / 后台订单管理

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • Order
  • vendor
  • sebastian
  • diff
  • src
  • LCS
  • TimeEfficientLongestCommonSubsequence...
TimeEfficientLongestCommonSubsequenceImplementation.php 1.91 KB
朱继来's avatar
Initial commit
41734920
 
朱继来 committed 7 years ago
1 2
<?php
/*
叶明星's avatar
账期管理
d99f4f05
 
叶明星 committed 6 years ago
3
 * This file is part of sebastian/diff.
朱继来's avatar
Initial commit
41734920
 
朱继来 committed 7 years ago
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace SebastianBergmann\Diff\LCS;

/**
 * Time-efficient implementation of longest common subsequence calculation.
 */
class TimeEfficientImplementation implements LongestCommonSubsequence
{
    /**
     * Calculates the longest common subsequence of two arrays.
     *
     * @param array $from
     * @param array $to
     *
     * @return array
     */
    public function calculate(array $from, array $to)
    {
        $common     = array();
叶明星's avatar
账期管理
d99f4f05
 
叶明星 committed 6 years ago
29 30
        $fromLength = \count($from);
        $toLength   = \count($to);
朱继来's avatar
Initial commit
41734920
 
朱继来 committed 7 years ago
31 32 33 34 35 36 37 38 39 40 41 42 43 44
        $width      = $fromLength + 1;
        $matrix     = new \SplFixedArray($width * ($toLength + 1));

        for ($i = 0; $i <= $fromLength; ++$i) {
            $matrix[$i] = 0;
        }

        for ($j = 0; $j <= $toLength; ++$j) {
            $matrix[$j * $width] = 0;
        }

        for ($i = 1; $i <= $fromLength; ++$i) {
            for ($j = 1; $j <= $toLength; ++$j) {
                $o          = ($j * $width) + $i;
叶明星's avatar
账期管理
d99f4f05
 
叶明星 committed 6 years ago
45
                $matrix[$o] = \max(
朱继来's avatar
Initial commit
41734920
 
朱继来 committed 7 years ago
46 47 48 49 50 51 52 53 54 55 56
                    $matrix[$o - 1],
                    $matrix[$o - $width],
                    $from[$i - 1] === $to[$j - 1] ? $matrix[$o - $width - 1] + 1 : 0
                );
            }
        }

        $i = $fromLength;
        $j = $toLength;

        while ($i > 0 && $j > 0) {
叶明星's avatar
账期管理
d99f4f05
 
叶明星 committed 6 years ago
57 58
            if ($from[$i - 1] === $to[$j - 1]) {
                $common[] = $from[$i - 1];
朱继来's avatar
Initial commit
41734920
 
朱继来 committed 7 years ago
59 60 61 62
                --$i;
                --$j;
            } else {
                $o = ($j * $width) + $i;
叶明星's avatar
账期管理
d99f4f05
 
叶明星 committed 6 years ago
63

朱继来's avatar
Initial commit
41734920
 
朱继来 committed 7 years ago
64 65 66 67 68 69 70 71
                if ($matrix[$o - $width] > $matrix[$o - 1]) {
                    --$j;
                } else {
                    --$i;
                }
            }
        }

叶明星's avatar
账期管理
d99f4f05
 
叶明星 committed 6 years ago
72
        return \array_reverse($common);
朱继来's avatar
Initial commit
41734920
 
朱继来 committed 7 years ago
73 74
    }
}