- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
给定一个常用词列表,按使用流行程度排序,是否有可能按照“最常见”序列的顺序形成任意长度的词组合(任何所需的词数)。例如,如果最常见的单词是“a、b、c”,那么对于长度为 2 的组合,将生成以下内容:
aa
ab
ba
bb
ac
bc
ca
cb
cc
这是长度为 3 的正确列表:
aaa
aab
aba
abb
baa
bab
bba
bbb
aac
abc
bac
bbc
aca
acb
bca
bcb
acc
bcc
caa
cab
cba
cbb
cac
cbc
cca
ccb
ccc
这对于任意数量的元素的 2 或 3 个单词(设置长度)的组合实现起来很简单,但是对于任意长度可以做到这一点吗?我想在 PHP 中实现它,但非常感谢伪代码或算法摘要!
最佳答案
这可能是您需要的递归函数。这个想法是,当给定一个长度和一个字母时,首先生成所有短一个字母但不包含该字母的序列。将新字母添加到末尾,您就得到了涉及该字母的序列的第一部分。然后将新字母向左移动。循环遍历每个字母序列包括右边的新字母。
所以如果你有 gen(5, d)开始于
(aaaa)d
(aaab)d
...
(cccc)d
然后当它完成 a-c 组合时它会做
(aaa)d(a)
...
(aaa)d(d)
(aab)d(d)
...
(ccc)d(d)
然后当它完成第 4 个字母 d 时,它将把它移到第 3 个
(aa)d(aa)
等等等等
<?php
/**
* Word Combinations (version c) 6/22/2009 1:20:14 PM
*
* Based on pseudocode in answer provided by Erika:
* http://stackoverflow.com/questions/1024471/generating-ordered-weighted-combinations-of-arbitrary-length-in-php/1028356#1028356
* (direct link to Erika's answer)
*
* To see the results of this script, run it:
* http://stage.dustinfineout.com/stackoverflow/20090622/word_combinations_c.php
**/
init_generator();
function init_generator() {
global $words;
$words = array('a','b','c');
generate_all(5);
}
function generate_all($len){
global $words;
for($i = 0; $i < count($words); $i++){
$res = generate($len, $i);
echo join("<br />", $res);
echo("<br/>");
}
}
function generate($len, $max_index = -1){
global $words;
// WHEN max_index IS NEGATIVE, STARTING POSITION
if ($max_index < 0) {
$max_index = count($words) - 1;
}
$list = array();
if ($len <= 0) {
$list[] = "";
return $list;
}
if ($len == 1) {
if ($max_index >= 1) {
$add = generate(1, ($max_index - 1));
foreach ($add as $addit) {
$list[] = $addit;
}
}
$list[] = $words[$max_index];
return $list;
}
if($max_index == 0) {
$list[] = str_repeat($words[$max_index], $len);
return $list;
}
for ($i = 1; $i <= $len; $i++){
$prefixes = generate(($len - $i), ($max_index - 1));
$postfixes = generate(($i - 1), $max_index);
foreach ($prefixes as $pre){
//print "prefix = $pre<br/>";
foreach ($postfixes as $post){
//print "postfix = $post<br/>";
$list[] = ($pre . $words[$max_index] . $post);
}
}
}
return $list;
}
?>
关于php - 在 PHP 中生成任意长度的有序(加权)组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1024471/
就类似于这个问题:mongodb query multiple pairs using $in 我想用 (first, last) >= ('John', 'Smith') 找到前 10 个全名。使用
如何保留向 NSDictionary 添加对象的方式? 我意识到 NSDictionary 中的值没有特定的顺序,但就我而言,我需要保留使用 setValue:forKey: 添加的顺序,例如一个数组
看看上证所运营商 CMPORDPS - ordered compare packed singles CMPUNORDPS - unordered compare packed singles 有序和
我使用 PowerMock 来模拟静态方法。我需要验证静态和非静态方法调用的顺序。可以使用 PowerMock 来做吗? UPD 我使用 powermockito 扩展来模拟静态方法,因此使用 pow
例如,如何合并两个已排序的整数流?我认为这是非常基本的,但只是发现它根本不是微不足道的。下面的不是尾递归的,当流很大时它会堆栈溢出。 def merge(as: Stream[Int], bs: St
我试图在二叉树中查找/打印每个节点的中序后继,但编译器给我的结果是段错误。 这是结构:- struct node { int x; struct node *left; str
我有一个查询看起来像 SELECT a, b, c, d FROM tab ORDER BY a ASC, b ASC 我的结果集看起来像 +-----------------
首先,我试过搜索这个主题但一无所获(似乎找不到合适的关键词),所以如果这是重复的,请告知。 我一直在尝试从我的数据库中获取一些 time_stamp 并将它们按时间间隔排序。例如,我运行一个查询,如
这个问题在这里已经有了答案: How do I get the index of an iterator of an std::vector? (9 个回答) 关闭 6 年前。 我已经订购了 QVe
我有以下实体,如果我尝试通过 removeTask 方法从 TaskList 中删除 Task,则会出现异常。 @Entity public class TaskList extends Generi
所以,我对 C 编程还是很陌生。 有3个长度相同的字符串。 str1="abc", str2="def", str3="ghi". 新字符串中的输出将类似于“adgbehcfi”。 #include
我的查询有一个问题,它花费的时间太长(仅仅这个简单的查询就超过了两秒)。 乍一看,这似乎是一个索引问题,所有连接的字段都已编入索引,但我找不到其他我可能需要编入索引以加快速度的内容。一旦我将我需要的字
我正在寻找一个 Map 实现,它按照键值对的添加顺序迭代它们。例如 Map orderedMap = // instantiation omitted for obvious reasons :) o
我正在寻找具有以下功能的数据库系统: 分层(多维)键 每个维度的键排序 因此,如果我的 key 类似于 App > User > Item,我可以运行如下查询:“该用户的下一项是什么?”或者“这个应用
以下类使用 CRTP 尝试将类型添加到具有 Schwarz 计数器以确保初始化顺序的 std::vector。根据 3.6.2/2 成员 h_ 具有无序初始化。我将如何更改它以确保它已订购初始化?我希
我正在实现一个玩具调度程序,它读取进程规范(例如到达时间、总运行时间)的输入文件,然后根据随机 io/cpu 突发调度进程。 文件格式 Arrival time, total CPU time, CP
我目前正在使用 python 2.7 requests 库,并且不支持有序 header 。我可以为 post 和 get 放置有序数据(如有序字典),但根本不支持标题。甚至在 python 3 中也
我正在使用来自 google guava 的 ConcurrentHashMap(通过 MapMaker),但该实现未排序。google guava 中有 ConcurrentSkipListMap,
我有一个旧应用程序,其中使用 ConcurrentHashMap。现在我们知道并发HasMap 是无序的,但是需要读取最初插入的对象。我已经在生产中使用了一段时间的代码,因此我正在寻找快速替代方案来替
最近我开始使用 .NET Core 2.1 开发一个新项目,我决定使用 SOLID 原则并创建一个漂亮的项目结构。 这是一个 Web API 项目。一切正常我使用了很多依赖注入(inject),大部分
我是一名优秀的程序员,十分优秀!