反转排列

时间:2018-01-18 11:28:24

标签: algorithm permutation

假设我有一个计算字符串排列的函数。将其称为perm(in, seq),其中in为任意字符串,seq为长度为strlen(str)的数字0,1,2,3...

perm输出字符串out out[i] == in[perm[i]]

示例:

perm("abc", "012") == "abc"
perm("abc", "210") == "cba"
perm("abc", "201") == "cab"

现在,我想找到序列seq_c,它是"逆" seq这样

perm(perm(str, seq), seq_c) == str

换句话说,找到将置换输入转换回原始输入的置换序列sec_c

2 个答案:

答案 0 :(得分:3)

array<int, 10> perm{ 2,7,4,9,8,3,5,0,6,1 };
array<int, 10> perm_inverse;
for (int i = 0; i < 10; ++i) perm_inverse[perm[i]] = i;
for (int i = 0; i < 10; ++i) cout << perm_inverse[i] << ",";
  

7,9,0,5,2,6,8,1,4,3,

答案 1 :(得分:0)

您可以简单地循环遍历序列并检查所有序列。

PSEUDO CODE:

While ( seq_c!= end )
{
if( perm(perm(str, seq), seq_c) == str)
return seq_c;
++seq_c;
}