📌  相关文章
📜  重新排列给定的字符串以最大化字符串t 的出现次数

📅  最后修改于: 2021-10-27 07:50:40             🧑  作者: Mango

给定两个二进制字符串st 。任务是以这样的方式重新排列字符串s ,使得字符串t作为s 中的子字符串的出现次数最多。

例子:

处理方法:如果我们不能使字符串T发生任何字符串s,然后输出S中的任意排列。否则,以t开始重新排列的字符串。现在我们将在字符串s中使字符串t的下一次出现尽可能靠左,以最大化出现次数。为了实现这一目标,我们会发现,字符串T前缀匹配字符串T最大的后缀
相同的长度。它可以通过 Prefix 函数、 Z-Function 或 Hashes 找到。

下面是上述方法的实现:

# Python3 implementation of the approach 
from collections import defaultdict
  
# Function to return the length of maximum 
# proper suffix which is also proper prefix
def Prefix_Array(t):
    m = len(t)
    arr =[-1]*m
    k =-1
    for i in range(1, m):
        while k>-1 and t[k + 1]!= t[i]:
            k = arr[k]
        if t[k + 1]== t[i]:
            k+= 1
        arr[i]= k
    return arr[-1]
  
# Function to return the rearranged string
def Rearranged(ds, dt):
    check = Prefix_Array(t)
  
    # If there is no proper suffix which is 
    # also a proper prefix
    if check ==-1:
        if ds['1']0:
                temp+='1'
                ds['1']-= 1
            while ds['0']>0:
                temp+='0'
                ds['0']-= 1
  
            # Return the rearranged string
            return temp
  
        # If count of 0's in string t is 0 
        if dt['0']== 0 and ds['1']!= 0:
            n = ds['1']//dt['1']
            temp = t * n
            ds['1']-= n * dt['1']
            while ds['1']>0:
                temp+='1'
                ds['1']-= 1
            while ds['0']>0:
                temp+='0'
                ds['0']-= 1
                  
            # Return the rearranged string
            return temp
              
        # If both 1's and 0's are present in
        # string t
        m1 = ds['1']//dt['1']
        m2 = ds['0']//dt['0']
        n = min(m1, m2)
        temp = t * n
        ds['1']-= n * dt['1']
        ds['0']-= n * dt['0']
        while ds['1']>0:
            temp+='1'
            ds['1']-= 1
        while ds['0']>0:
                temp+='0'
                ds['0']-= 1
        return temp
  
    # If there is a suffix which is 
    # also a prefix in string t
    else:
        if ds['1']0:
                temp+='1'
                ds['1']-= 1
            while ds['0']>0:
                temp+='0'
                ds['0']-= 1
            return temp
  
        # If count of 1's in string r is 0
        if dr['1']== 0 and ds['0']!= 0:
            n = ds['0']//dr['0']
            temp+= r * n
            ds['0']-= n * dr['0']
            while ds['1']>0:
                temp+='1'
                ds['1']-= 1
            while ds['0']>0:
                temp+='0'
                ds['0']-= 1
            return temp
  
        # If count of 0's in string r is 0
        if dr['0']== 0 and ds['1']!= 0:
            n = ds['1']//dr['1']
            temp+= r * n
            ds['1']-= n * dr['1']
            while ds['1']>0:
                temp+='1'
                ds['1']-= 1
            while ds['0']>0:
                temp+='0'
                ds['0']-= 1
            return temp
  
        # If string r have both 0's and 1's
        m1 = ds['1']//dr['1']
        m2 = ds['0']//dr['0']
        n = min(m1, m2)
        temp+= r * n
        ds['1']-= n * dr['1']
        ds['0']-= n * dr['0']
        while ds['1']>0:
            temp+='1'
            ds['1']-= 1
        while ds['0']>0:
                temp+='0'
                ds['0']-= 1
        return temp
  
# Driver code
if __name__=="__main__":
    s ="10101000"
    t ="101"
  
    # Count of 0's and 1's in string s
    ds = defaultdict(int)
  
    # Count of 0's and 1's in string t
    dt = defaultdict(int)
    for i in s:
        ds[i]= ds[i]+1
    for i in t:
        dt[i]= dt[i]+1
    print(Rearranged(ds, dt))
输出:
10101000

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程