【问题标题】:I want to know the approach to compress any string我想知道压缩任何字符串的方法
【发布时间】:2015-10-28 17:25:36
【问题描述】:

我想知道方法,我怎么能compress 字符串,就像我给了一些字符串 ABCABCABC 一样,因为我认为我可以找到子字符串 ABC 这是frequently occurs 所以它将被压缩为3ABC。如果给出像ABCABCBC 这样的字符串的另一种方式是比这里的ABCfrequently occurring 子字符串,所以它被压缩为2ABC1BC。如您所见,我只考虑adjacent substrings

【问题讨论】:

  • 通过pigeonhole principle,任何使某些字符串变短的无损压缩算法必然会使某些其他字符串变大。因此,不幸的是,“压缩任何字符串”是一个不可能实现的目标。
  • 谢谢 Jaimin 么。代码运行良好。

标签: algorithm performance string-comparison substring


【解决方案1】:
s='FLFLAFLAFLAF'


def check(str_check,str):

    try:
        index=str.index(str_check)
    except Exception:
        index=9999
    return index

def max_index(i,j,sub_str):
    if(i>=0 and j<=len(count)):
        try:
            j=count.index(max(count[i:j]))
        except Exception:
            j=-1
        try:
            sub_str_index=int(sub_str.index(sub_list[j]))
        except Exception:
            sub_str_index=-1
        temp=int(count[j])
        if len(sub_str)==0:
            return ""
        elif(count[j]==1 or int(count[j])*len(sub_list[j])>len(sub_str)):
            for i in range (len(sub_str)):
                count[j+i]=0
            return "1"+ sub_str
        else:
            #count[j]=0
            return max_index(0,sub_str_index,sub_str[0:sub_str_index]) + str(temp) + sub_list[j] + max_index(len(sub_str[0:sub_str_index])+(int(temp)*len(sub_list[j])),len(sub_str),sub_str[len(sub_str[0:sub_str_index])+(int(temp)*len(sub_list[j])): len(sub_str)])







sub_list=sorted(set([s[i:i+j] for j in range(1,len(s)+1) for i in range(len(s)-j+1)]))
length=len(s)
length1=len(sub_list)
count=[]
for i in range (length1):
    cnt=0
    j=0
    while(j<length):
        k=check(sub_list[i],s[j:])
        if k==9999:
            break
        if (s[j+k:j+k+len(sub_list[i])]==sub_list[i]):
            if(cnt>=1 and s[j+k-len(sub_list[i]):j+k]==sub_list[i]):
                j=j+k+1
                cnt=cnt+1
            elif (cnt==0):
                j=j+k+1
                cnt=cnt+1
            else:
                j=length
        else:
            j=length
    count.append(cnt)


print "SUBLIST"
print sub_list
print "count"
count1=count
print count1
j=len(count)
i=0
max_ele=max(count)
while(max_ele in count):
    max_in=count.index(max(count[i:j]))
    print max_index(i,j,s)
    count=count1
    count[max_in]=0

【讨论】:

  • "FLFLAFLAFLAF" 如果你给出这种类型的输入,那么你会得到如下输出:1FL3FLA1F 1FLF3LAF
猜你喜欢
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-26
  • 2023-03-16
相关资源
最近更新 更多