How to find the maximum sequence of characters at the end of an array that match the beginning of an array?

I want to write code that finds the maximum sequence of characters at the end of an array that match the beginning of the array.

But I do not know how I can do this with PHP?

For instance:

Input = [a,b,c,e,r,t,x,s,b,a,b,c] 
Output = [a,b,c]

(since the elements a,b,care both at the beginning and at the end of the array and represent the maximum sequence of such characters)

+4
source share
3 answers

Note. . This works great for such an array, where we have an array of strings, it does not work for a nested array.

Try this piece of code here

<?php
ini_set('display_errors', 1);
$data  = array("a","b","c","e","r","t","x","s","b","a","b","c"); 
$string=  implode("", $data);//converting array to string.
for($x=strlen($string)-1;$x>=0;$x--)
{
    //matching substring from the end of string.
    if(preg_match("/".substr($string, 0,$x)."$/",$string)==true)
    {
        $string= substr($string, 0,$x);
        break;
    }
}
$result=str_split($string);
print_r($result);
+3

, :

 <?php 
   $Input  = array('a','b','c','e','r','t','x','s','b','a','b','c'); 
    $len=count($Input);
    $j=$len-1;
    $count=0;
    $s=0;
    $k=$n=0;
    $a[$len/2];
    for($i=0;$i<$len;$i++)
    {
        if($Input[$i]!=$Input[$j]){
            $j--;
            $i--;
        }
        if($Input[$i]==$Input[$j]){
            $count++;
            $a[$n]=$Input[$j];
            $n++;
            if($k==$j)
            {
                $s++;
                break;
            }
            $k=$j;

            if($j!=$len-1)
                $j++;
            else
                break;
        }
    }
    if($s!=0)
        echo "sequence not present";
    else
        {
            echo "<br>sequence present <br>";
            $len2=count($a);
            for($p=0;$p<$len2;$p++)
                echo" ".$a[$p];
        }

    ?>
+3

, !

:

$found=false;                                              // declare default outcome
for($x=1,$max=sizeof($data); $x<=$max; ++$x){              // this allows "overlap"
    if(array_slice($data,0,$x)===array_slice($data,-$x)){  // compare start to end
        $found=true;                                       // declare a match has occurred
    }elseif($found){                                       // this iteration is no match
        --$x;                                              // rewind to successful match
        break;
    }
}
var_export($found?array_slice($data,0,$x):"No match");      // output the result

:

$data=['a','b','c','e','r','t','x','s','b','a','b','c'];  // ['a','b','c']
$data=['n','o','p','e'];                                  // No Match
$data=['r','a','c','e','c','a','r'];                      // ['r']
$data=['a','a','b','a','a'];                              // ['a','a']

:

, . , , ( ).

array_slice() . $x , array_slice() , .

$max "" . "", $max=floor(sizeof($data)/2)

, , , , .


...

Palindromic Matching - you can easily customize my method above to match mirror sequences by adding array_reverse().

Method:

$found=false;
for($x=1,$max=sizeof($data); $x<=$max; ++$x){
    if(array_slice($data,0,$x)===array_reverse(array_slice($data,-$x))){  // only change
        $found=true;
    }elseif($found){
        --$x;
        break;
    }
}
var_export($found?array_slice($data,0,$x):"No match");

Inputs and outputs:

$data=['a','b','c','e','r','t','x','s','b','a','b','c'];    // No Match
$data=['n','o','p','e'];                                    // No Match
$data=['r','a','c','e','c','a','r'];                        // ['r','a','c','e','c','a','r']
$data=['a','a','b','a','a'];                                // ['a','a','b','a','a']
+1
source

Source: https://habr.com/ru/post/1674955/


All Articles