【问题标题】:Find unique permutations from array of words, limiting a set of words by 12 characters从单词数组中查找唯一排列,将一组单词限制为 12 个字符
【发布时间】:2021-02-16 19:23:34
【问题描述】:

让我解释一下,我需要这个来根据一组预定义的单词开发一个独特的标题生成器。

例如,我有这个单词列表:

$list = ['苹果', '香蕉', '梨'];

我对标题的大小有限制,例如:1 个字符 如果我生成所有排列的列表,我将拥有:

apple
banana
pear
apple banana
apple pear
apple banana pear
banana apple
banana pear
banana apple pear
pear apple
pear banana
pear banana apple

但我不想要这个,任何规则是:单词不能在另一组单词中再次重复,我只想要最大的一组单词

结果是:

apple banana
apple pear
banana pear

我已经尝试了以下解决方案,但这些都没有帮助:

PHP algorithm to generate all combinations of a specific size from a single set

PHP Find All (somewhat) Unique Combinations of an Array

Every (specific sized) combination from set/array with no duplicate items

Efficient PHP algorithm to generate all combinations / permutations of inputs

How do you generate a list of all possible strings given a generator of characters and a length?

我有这段代码,但它没有按我的意愿删除重复项

public static function search_get_combos($array = array(), $maxCaracters=12) {

        sort($array);

        $terms = array();

        for ($dec = 1; $dec < pow(2, count($array)); $dec++) {
            $curterm = array();
            foreach (str_split(strrev(decbin($dec))) as $i => $bit) {
                if ($bit) {
                    $curterm[] = $array[$i];
                }
            }
            if (!in_array($curterm, $terms) && count($curterm) > 1) {
                $title = implode(' ', $curterm);
                if (strlen($title) <= $maxCaracters){
                    $terms[$title] = $curterm;
                }
            }
        }

        return $terms;

    }

输出:

array(6) {
  ["Apple"]=>
  array(1) {
    [0]=>
    string(5) "Apple"
  }
  ["Banana"]=>
  array(1) {
    [0]=>
    string(6) "Banana"
  }
  ["Apple Banana"]=>
  array(2) {
    [0]=>
    string(5) "Apple"
    [1]=>
    string(6) "Banana"
  }
  ["Pear"]=>
  array(1) {
    [0]=>
    string(4) "Pear"
  }
  ["Apple Pear"]=>
  array(2) {
    [0]=>
    string(5) "Apple"
    [1]=>
    string(4) "Pear"
  }
  ["Banana Pear"]=>
  array(2) {
    [0]=>
    string(6) "Banana"
    [1]=>
    string(4) "Pear"
  }
}

【问题讨论】:

  • 您说“我只想要最大的一组词
  • 对,说错了^^

标签: php permutation


【解决方案1】:

你是这个意思吗?

<?php

$list = ['apple', 'banana', 'pear', 'a'];

$result = [];

foreach($list as $k => $v) {
    unset($list[$k]);
    foreach($list as $subv) {
        $word_combination = $v.' '.$subv;
        if (strlen($word_combination) >= 10) {
            array_push($result, $v.' '.$subv);
        }
    }
}

print_r($result);
print(implode(' ', $result));

输出:

Array
(
    [0] => apple banana
    [1] => apple pear
    [2] => banana pear
)
apple banana apple pear banana pear

我不明白

我只想要最大的一组单词

您的意思是 apple banana 必须超过 10 个字符(apple banana = 12 个字符)? 如果是,我添加了检查。例如apple a(7 个字符)将被忽略。

【讨论】:

  • 例如,我生成这个列表: 苹果香蕉梨 苹果香蕉苹果香蕉 苹果香蕉梨 > 10 个字符,删除它 苹果香蕉
  • “苹果香蕉
  • 对不起,我说错了,不是10个字符,是12个字符。我将尝试以另一种方式解释它:假设生成的标题将是: Women's Shoes Women's Shoes 由于前一个标题中重复了 women's pump shoes,因此必须将其删除。最后我需要的是,它管理所有可能的标题组合,仅保留 60 个字符以下的最大标题,必须从列表中删除所有单词都在较大标题内的任何其他标题组合,仅结束最大的标题少于 60 个字符。
  • @DilneiSoetheSpancerski 好的。您可以在我的解决方案中将 strlen($word_combination) &gt;= 10 更改为 strlen($word_combination) &gt;= 12,并且只会显示超过 12 个符号的组合。至少我的输出与您的问题The result would be: 相同
猜你喜欢
  • 1970-01-01
  • 2011-08-26
  • 2020-01-16
  • 2016-08-09
  • 2020-01-01
  • 2013-04-08
  • 2020-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多