I just encountered a tricky algorithm problem.-php Tutorial

Source: Internet
Author: User
I just encountered a tricky algorithm problem and solved it.


Reply to discussion (solution)

$a = array(1,2,2,3,1,2,3,1,1);$st = array(1 => 0);$d = 1;foreach($a as $v) {  @$st[$v]++;  if($v >= $d) {    $d = $v;  }else{    while($v < $d) {      unset($st[$d--]);    }  }  $r[] = join('.', $st);}print_r($r); 
Array
(
[0] => 1
[1] => 1.1.
[2] => 1.2.
[3] => 1.2.1
[4] => 2
[5] = & gt; 2.1
[6] => 2.1.1
[7] => 3
[8] => 4
)

Sorry, I didn't see what it meant. ........ .... ............. ........ .... ........ .... ....... ....... .........

$a = array(1,2,2,3,1,2,3,1,1);$st = array(1 => 0);$d = 1;foreach($a as $v) {  @$st[$v]++;  if($v >= $d) {    $d = $v;  }else{    while($v < $d) {      unset($st[$d--]);    }  }  $r[] = join('.', $st);}print_r($r); 
Array
(
[0] => 1
[1] => 1.1.
[2] => 1.2.
[3] => 1.2.1
[4] => 2
[5] = & gt; 2.1
[6] => 2.1.1
[7] => 3
[8] => 4
)


Thank you for your answers. if you can, could you tell me your thoughts? thank you very much.

Sorry, I didn't see what it meant. ........ .... ............. ........ .... ........ .... ....... ....... .........


This is actually a directory arrangement problem. you can see that the priority determines the display of the size and title.

$ A = array (, 1); indicates the hierarchy of nodes
$ St = array (1 => 0); stores the current serial number of each layer
$ D indicates the current level

Well, thanks to the moderator for his explanation. I will study it and send it to you first.

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.