String 最短字符串,包含字符列表中所有固定长度的子字符串(子集的任何1个排列)
如果给我一个字符列表,比如{s1,s2,s3,…,s10},我想找到一个最短长度的字符串,其中所有长度为3的无序子集组合都作为字符串中的子字符串出现。例如,如果我考虑子集{S2,S4,S9},那么我就能够定位至少一个包含任何三个字符的字符串作为子串的一个实例。没有重复,因为不需要包含形式为“s1s1s1”的子字符串。我使用约束解算器解决了这个问题:String 最短字符串,包含字符列表中所有固定长度的子字符串(子集的任何1个排列),string,algorithm,graph-algorithm,String,Algorithm,Graph Algorithm,如果给我一个字符列表,比如{s1,s2,s3,…,s10},我想找到一个最短长度的字符串,其中所有长度为3的无序子集组合都作为字符串中的子字符串出现。例如,如果我考虑子集{S2,S4,S9},那么我就能够定位至少一个包含任何三个字符的字符串作为子串的一个实例。没有重复,因为不需要包含形式为“s1s1s1”的子字符串。我使用约束解算器解决了这个问题: % dimensions int: N = 10; % number of characters set of int: Characters
% dimensions
int: N = 10; % number of characters
set of int: Characters = 1..N;
int: L = 416; % length of shortest string
% decision variables
array[0..L-1] of var Characters: shortest;
% every unordered subset must occur somewhere in shortest
constraint forall(a, b, c in 1..N where (a < b) /\ (b < c)) (
exists(i in 0..L-3) (
((shortest[i] == a) \/(shortest[i+1] == a) \/ (shortest[i+2] == a)) /\
((shortest[i] == b) \/(shortest[i+1] == b) \/ (shortest[i+2] == b)) /\
((shortest[i] == c) \/(shortest[i+1] == c) \/ (shortest[i+2] == c))
)
);
% to speed things up, we enforce the first N entries
constraint forall(i in 0..N-1) (
shortest[i] == i+1
);
% further speedup: adjacent entries are probably different
constraint forall(i in N..L-2) (
shortest[i] != shortest[i+1]
);
solve satisfy;
%
% Output solution as table of variable value assignments
%%
output
[ show(shortest[i]) ++ " " | i in 0..L-1 ];
但是对于更多的字符,更不用说10个了,搜索时间太长,不实用
我注意到,每增加一个字符,最小长度似乎大约是原来的两倍。
对于3个字符,长度仅为3。对于4个字符,它是6个字符,对于5个字符,它是13个字符。但我找不到6个或更多字符的解决方案
我找到了一篇相关论文,证实了我对5个字符的发现。但这篇论文发表于1978年。可能还有更多的最新发现。分配得不错。到目前为止你都做了些什么?不比brute好,这不值得在这里发布。
1 2 3 4 5 1 2 4 1 3 5 2 4