给定一组N个唯一排序的字符,
如何找到可以连接到原始有序字符集(在R中)的所有子字符串组合?
例如,对于n=5,使用以a开头的字母字符,输入(作为字符元素)和期望的输出(作为字符元素的向量列表)将如下所示,
输入:
ordered.chars <- "abcde"
所需输出:
ord.substr.list <- list(
c("a","b","c","d","e"),
c("ab","c","d","e"),
c("ab","cd","e"),
c("ab","c","de"),
c("a","bc","d","e"),
c("a","bc","de"),
c("a","b","cd","e"),
c("a","b","c","de"),
c("abc","d","e"),
c("abc","de"),
c("a","bcd","e"),
c("a","b","cde"),
c("ab","cde"),
c("abcd","e"),
c("a","bcde"))
对所有列出的字符元素向量连接成原始字符元素的条件的一种测试:
all(unlist(lapply(ord.substr.list, function(x) paste(x, collapse=""))) %in% ordered.chars)
我的google/stackoverflow搜索得到了combn(),它在类似的情况下很有用,但在这里似乎没有什么明显的帮助。
发布于 2019-09-16 17:38:19
您的问题的核心是能够生成power set。
这是一个使用RcppAlgos
(我是作者)的解决方案。
library(RcppAlgos)
customPowSetStr <- function(n) {
len <- n * 2 - 1
v <- vector("character", length = len)
v[seq(1, len, 2)] <- letters[1:n]
v[seq(2, len, 2)] <- ","
comboGeneral(0:(n - 1), n - 1, freqs = c(n - 2, rep(1, n - 1)), FUN = function(x) {
temp <- v
strsplit(paste0(temp[-(x[x > 0] * 2)], collapse = ""), ",")[[1]]
})
}
customPowSetStr(5)
[[1]]
[1] "ab" "c" "d" "e"
[[2]]
[1] "a" "bc" "d" "e"
[[3]]
[1] "a" "b" "cd" "e"
[[4]]
[1] "a" "b" "c" "de"
[[5]]
[1] "abc" "d" "e"
[[6]]
[1] "ab" "cd" "e"
[[7]]
[1] "ab" "c" "de"
[[8]]
[1] "a" "bcd" "e"
[[9]]
[1] "a" "bc" "de"
[[10]]
[1] "a" "b" "cde"
[[11]]
[1] "abcd" "e"
[[12]]
[1] "abc" "de"
[[13]]
[1] "ab" "cde"
[[14]]
[1] "a" "bcde"
[[15]]
[1] "abcde"
https://stackoverflow.com/questions/57959707
复制