【问题标题】:All combinations of substrings that can be concatenated into the original string in R可以连接到R中原始字符串的所有子字符串组合
【发布时间】:2020-01-17 11:01:54
【问题描述】:

给定一组 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(),这在类似情况下很有用,但在这里似乎没有明显帮助。

【问题讨论】:

    标签: r string substring combinatorics


    【解决方案1】:

    问题的核心是能够生成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"
    

    【讨论】:

    • 这是一个非常简洁的解决方案!关于电源组的重要链接。我希望创建这些数据有一些有趣的理论方面,我不会失望。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    相关资源
    最近更新 更多