dpbvpgvrhwxen3222 2010-07-27 05:29
浏览 60
已采纳

如何优化从php中的输入匹配多部分rar文件的算法

I'm looking for a better optimized way to find and group multipart archives from an array of filenames

I have as an input for example:

array(
books.part1.rar,
books.part3.rar,
00000114.rar,
svoy_20ostrov.rar,
svoy_20ostrov.rar,
koncert_20v_20dk_20mir.rar,
koncert_20v_20centralnom_20teatre_20kukol.rar,
LP_LIVE_PR_Tampa.part2.rar,
koncert_20v_20dk_20vami.rar,
koncert_20v_20dk_20kommuna_20chast1.rar,
books.part2.rar,
koncert_20v_20dk_20kommuna_20chast2.rar,
books.part4.rar,
recedivist.rar,
LP_LIVE_PR_Tampa.part1.rar
)

And I'm looking for the output

array(  

array(

books.part1.rar
books.part2.rar
books.part3.rar
books.part4.rar ) ,

00000114.rar
svoy_20ostrov.rar
koncert_20v_20dk_20mir.rar
koncert_20v_20centralnom_20teatre_20kukol.rar
koncert_20v_20dk_20vami.rar

array(
koncert_20v_20dk_20kommuna_20chast1.rar
koncert_20v_20dk_20kommuna_20chast2.rar
)

recedivist.rar
array (
LP_LIVE_PR_Tampa.part1.rar
LP_LIVE_PR_Tampa.part2.rar
)
)

I'm using php as a programming language, by the way,

An idea was to match with a regular expression files like (.+).part1.rar then when found , match all the other part([0-9]+).rar (other foreach required that loops through all array) and when found unset() those entries and add them to the new constructed array

  • 写回答

2条回答 默认 最新

  • doucaishou0074 2010-07-27 06:18
    关注

    I would sort the array first and then loop through it, performing the Levenshtein() function on the next entry.

    $rars = array(
        books.part1.rar,
        books.part3.rar,
        00000114.rar,
        svoy_20ostrov.rar,
        svoy_20ostrov.rar,
        koncert_20v_20dk_20mir.rar,
        koncert_20v_20centralnom_20teatre_20kukol.rar,
        LP_LIVE_PR_Tampa.part2.rar,
        koncert_20v_20dk_20vami.rar,
        koncert_20v_20dk_20kommuna_20chast1.rar,
        books.part2.rar,
        koncert_20v_20dk_20kommuna_20chast2.rar,
        books.part4.rar,
        recedivist.rar,
        LP_LIVE_PR_Tampa.part1.rar
    )
    
    sort($rars);
    $current = 0;
    $rars_complete = array();
    foreach($rars as $i=>$rar) {
        $next = ($i + 1) < count($rars)) ? $i + 1 : false;
        $rars_complete[$current][] = $rar;
        if($next != false && levenshtein($rar, $rars[$next]) == 1)
            continue;
        else
            $current++;
    }
    

    Note, this is not tested.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路