【问题标题】:Algorithm to convert string from one format to other将字符串从一种格式转换为另一种格式的算法
【发布时间】:2017-02-02 13:09:47
【问题描述】:

我正在查看一个问题,该问题声明如下转换字符串。

s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

我能够理解如何做到这一点。

我在想有没有办法在 reverse 中做到这一点。当给定一个像abcabccdcdcdef 这样的字符串时,我知道可以有很多表示的可能性。我在寻找我们可以用占用最低内存的表示来做到这一点(不是算法,而是最终字符串)。

【问题讨论】:

  • 不,不是作业。我在做this
  • 查看较早的问题。您的情况似乎更复杂(带有嵌套和括号),但 AFAICT 这种方法也应该适用于您。
  • 我在想这个答案是否适用于嵌套?
  • @user168983 它可能可以适应,但代价是变成三次或四次。

标签: algorithm


【解决方案1】:

为了最大限度地提高效率,我们希望尽可能减少。我想我会做这样的事情(它可能不是最有效的算法):

s = "whateverwhateveryouwantwantwantababababababababc"
possibilities = []
repeats = []
def findRepeats(repeats, s, length):
    for i in range(0, len(s) - 2 * length + 1):
        if s[i:i+length] == s[i+length:i+2*length]:
            trackInd = i+length
            times = 2
            while trackInd+2*length <= len(s):
                if (s[trackInd:trackInd+length]==s[trackInd+length:trackInd+2*length]):
                    times += 1
                else: break
                trackInd += length

            repeats.append((i, times, s[i:i+length]))

    return repeats

for i in range(0, len(s)):
    repeats = findRepeats(repeats, s, i)

def formPossibility(repeats, s):
    build = ""
    i = 0
    while i < len(s):
        pass = True
        for repeat in repeats:
            if repeat[0] == i:
                pass = False
                build += repeat[1] + "["
                build += repeat[2] + "]"
                break

        if pass:
            build += s[i]

# I didn't finish this but you would loop through all the repeats and test
# them to see if they overlap, and then you would take all the posibilities
# of different ways to make them so that some are there, and some are not.
# in any case, I think that you get the idea.
# I couldn't finish this because I am doing the coding on stackoverflow and
# its like so painful and so hard to debug. also I don't have enough time sorry

【讨论】:

    【解决方案2】:

    不知道是最高效还是根本不高效,但这是我使用js的方法。

    function format(pattern, length, times) {
        var result = "";
        if (times == 0) {
            result = pattern;
        } else {
            result = (times + 1).toString() + "[" + pattern + "]";
        }
        return result;
    }
    
    function encode(input) {
        var result = "";
        var pattern = { length: 1, times: 0 };
        var i = 1;
        while (i <= input.length / 2) {
            var subpattern = input.substr(0, i);
            var j = 0;
            while (input.substr(i + j * i, i) == subpattern && j + i < input.length) {
                j++;
            }
            if (i * j > pattern.length * pattern.times) {
                pattern.length = i;
                pattern.times = j;
            }
            i++;
        }
        if (pattern.length > 1) {
            result = format(encode(input.substr(0, pattern.length)), pattern.length, pattern.times);
        } else {
            result = format(input.substr(0, pattern.length), pattern.length, pattern.times);
        }
        if (pattern.length + pattern.length * pattern.times < input.length) {
            result += encode(input.substr(pattern.length + pattern.length * pattern.times, input.length));
        }
        return result;
    }

    【讨论】:

      猜你喜欢
      • 2018-01-20
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多