Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 一种求序列排列的算法_Algorithm - Fatal编程技术网

Algorithm 一种求序列排列的算法

Algorithm 一种求序列排列的算法,algorithm,Algorithm,我在问一个简单的问题:如何在一个数字序列(重复)中找到一个(并且只有一个)复杂度最低的排列 假设我们有序列:1234。然后我们排列2和3,所以我们有:13124。我怎么能发现2和3已经被排列了呢?最糟糕的解决方案是生成所有可能性,并将每个可能性与原始置换序列进行比较,但我需要一些快速的 谢谢您的回答。函数findswaps: function findswaps: linkedlist old <- store old string in linkedlist linked

我在问一个简单的问题:如何在一个数字序列(重复)中找到一个(并且只有一个)复杂度最低的排列

假设我们有序列:
1234
。然后我们排列2和3,所以我们有:
13124
。我怎么能发现2和3已经被排列了呢?最糟糕的解决方案是生成所有可能性,并将每个可能性与原始置换序列进行比较,但我需要一些快速的

谢谢您的回答。

函数findswaps:
function findswaps:
    linkedlist old <- store old string in linkedlist
    linkedlist new <- store new string in linkedlist

    compare elements one by one:
        if same
             next iteration until exhausted
        else
             remember old item
             iterate through future `new` elements one by one:
                 if old item is found
                     report its position in new list
                 else
                     error

linkedlist old问题在于,您的问题将有多个解决方案,而不受某些约束,例如顺序是按顺序找到的

我要看的是第一个测试,序列中仍然有相同的值,如果是的话,只需一步一步地进行,直到发现不匹配,然后找到另一个值第一次出现的位置,并将其标记为置换。现在继续搜索下一个修改,等等

如果你只是想知道它改变了多少,我会看看levenshtein算法。该算法的基础甚至可以为您提供自定义算法所需的内容,或启发其他方法

这很快,但它不会告诉您哪些项目已更改


我所知道的唯一完整的解决方案是在每次更改发生时进行记录,这样您就可以查看更改的历史记录以了解完美的答案。

如果原始数组和派生数组之间只有1次交换,您可以在数组长度n为O(n)时尝试类似的方法:

int count = 0;
int[] mismatches;

foreach index in array {
    if original[index] != derived[index] {
        if count == 2 {
            fail
        }
        mismatches[count++] = index;
    }
}

if count == 2 and
   original[mismatches[0]] == derived[mismatches[1]] and
       original[mismatches[1]] == derived[mismatches[0]] {

    succeed
}

fail

请注意,当阵列之间没有交换任何内容时,这会报告失败。

坦白地说,我不理解这个问题。您是否在寻找从序列1到序列2所需的最小交换?您可以比较元素本身和元素数量是否相同。生成所有要检测的置换(在您的示例中只有8个,IICC)并为其生成DFA。(f) 莱克斯是你的朋友。