Algorithm 将字符串从一种格式转换为另一种格式的算法
我正在研究一个问题,该问题声明如下转换字符串Algorithm 将字符串从一种格式转换为另一种格式的算法,algorithm,Algorithm,我正在研究一个问题,该问题声明如下转换字符串 s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return "accaccacc". s = "2[abc]3[cd]ef", return "abcabccdcdcdef". 我能够理解如何做到这一点 我在想有没有一种相反的方法。当给定一个像abcabcdcdef这样的字符串时,我知道可以有很多表示的可能性。我在想,我们能不能用内存最少的表示法来表示,不是算法,而是最后一个字符串。为了获得最
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
我能够理解如何做到这一点
我在想有没有一种相反的方法。当给定一个像abcabcdcdef这样的字符串时,我知道可以有很多表示的可能性。我在想,我们能不能用内存最少的表示法来表示,不是算法,而是最后一个字符串。为了获得最大的效率,我们希望尽可能地减少内存。我想我会这样做它可能不是最有效的算法:
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
我不知道它是最有效的还是最有效的,但下面是我使用js的方法 函数格式模式、长度、时间{ var结果=; 如果时间==0{ 结果=模式; }否则{ 结果=次数+1.toString+[+模式+]; } 返回结果; } 函数编码输入{ var结果=; var模式={length:1,times:0}; var i=1; 而我是pattern.length*pattern.times{ pattern.length=i; pattern.times=j; } i++; } 如果pattern.length>1{ 结果=formatencodeinput.substr0,pattern.length,pattern.length,pattern.times; }否则{ 结果=formatinput.substr0,pattern.length,pattern.length,pattern.times; } 如果pattern.length+pattern.length*pattern.times