Test question algorithm to improve the operation of the string (pyhton detailed version)

Put the question requirements first, and explain them through the procedures one by one, so that you can thoroughly understand my thoughts, and I hope to help you.


author: Xiao Huang
Slow and firm growth


资源限制
时间限制:1.0s   内存限制:256.0MB
问题描述
  给出一个字符串S,然后给出q条指令,分别有4种:

  1. Append str
  表示在S的最后追加一个字符串str。
  例:
  原字符串:ABCDE
  执行 Append FGHIJ 后
  字符串变为:ABCDEFGHIJ

  2. Insert x str
  表示在位置x处插入一个字符串str。(输入保证0<x<=当前字符串长度)
  例:
  原字符串:ABCGHIJ
  执行 Insert 4 DEF 后
  字符串变为:ABCDEFGHIJ

  3. Swap a b c d
  表示交换从第a位到第b位的字符串与从第c位到第d位的字符串。(输入保证0<a<b<c<d<=当前字符串长度)
  例:
  原字符串:ABGHIFCDEJ
  执行 Swap 3 5 7 9后
  字符串变为:ABCDEFGHIJ

  4. Reverse a b
  表示将从第a位到第b位的字符串反转。(输入保证0<a<b<=当前字符串长度)
  例:
  原字符串:ABGFEDCHIJ
  执行 Reverse 3 7 后
  字符串变为:ABCDEFGHIJ

  最后输出按顺序执行完指令后的字符串。
输入格式
  输入第一行包含字符串S,第二行包含一个整数q,接下来q行分别为q个指令。
输出格式
  输出为1行,为按顺序执行完输入指令后的字符串。
样例输入
My
5
Append Hello
Insert 3 dlroW
Reverse 3 7
Swap 3 7 8 12
Swap 1 2 3 7
样例输出
HelloMyWorld
样例说明
  原字符串:My
  执行 Append Hello 后:MyHello
  执行 Insert 3 dlroW 后:MydlroWHello
  执行 Reverse 3 7 后:MyWorldHello
  执行 Swap 3 7 8 12 后:MyHelloWorld
  执行 Swap 1 2 3 7 后:HelloMyWorld
数据规模和约定
  对于30%的数据,q=1;

  对于70%的数据,如有Swap指令,Swap指令中b-a=d-c;

  对于100%的数据,最终字符串长度不大于400001<=q<=150

Main function:
S is the input string, and I turn it into a list here because the list's own function is used in the Append () function and Insert () function. Of course, it is not necessary to turn it into a list, just modify the corresponding operation.
'' .jion (S) means to seamlessly convert the list into a character string.
Example: '' .jion (['1', '2', '3', '4', '5', '6']) will become 123456
n is the q in the title

if __name__ == "__main__":
        S = list(input())
        n = int(input())
        if 1<=n<=150:
                while n > 0:
                        nums = input()
                        S = fun(S,nums)
                        n -= 1
        print(''.join(S))

About the Append function:
add s after the original S, here is the append () function using the list, and add the elements in s one by one after S, where the time complexity of append () is O (1).

def Append(S,s):
        for i in s:
                S.append(i)

About the Insert () function:
insert s at the x position of the original string S, and insert using the insert () function of the list.

def Insert(S,x,s):
        for i in range(len(s)):
                S.insert(x-1+i,s[i])

Regarding the Swap () function: It is
considered here that the different positions of d will have different effects on the slicing operation of the list, so it is divided into two cases of d = n and d <n.

def Swap(S,a,b,c,d):
        n = len(S)
        if n > d:
                n1 = S[0:a-1]
                n2 = S[a-1:b]
                n3 = S[b:c-1]
                n4 = S[c-1:d]
                n5 = S[d:]
                S = n1+n4+n3+n2+n5
        elif n == d:
                n1 = S[0:a-1] 
                n2 = S[a-1:b]
                n3 = S[b:c-1]
                n4 = S[c-1:]
                S =  n1+n4+n3+n2
        return S

Reverse () function:
here we must consider the size of two values ​​a and b, discuss the impact of the value of b on this function in the case of a = 1 and a ≠ 1, respectively. For example, when a = 1, b = n, then simply flip this function.

def Reverse(S,a,b):
        n = len(S)
        if a > 1:
                if b < n:
                        n1 = S[:a-1]
                        n2 = S[a-1:b]
                        n3 = S[b:]
                        S = n1 + n2[::-1] +n3
                elif b == n:
                        n1 = S[:a-1]
                        n2 = S[a-1:]
                        S = n1 + n2[::-1]
        else:
                if b < n:
                        n1 = S[:b]
                        n2 = S[b:]
                        s = n1[::-1] + n2
                else:
                        S = S[::-1]
        return S

Regarding the fun () function:
In this function, the value of each input is judged and the corresponding operation is performed.

def fun(S,nums):
        a = nums.split()
        if a[0] == 'Append':
                Append(S,a[1])
        elif a[0] == 'Insert':
                Insert(S,int(a[1]),a[2])
        elif a[0] == "Swap":
                S = Swap(S,int(a[1]),int(a[2]),int(a[3]),int(a[4]))
        elif a[0] == "Reverse":
                S = Reverse(S,int(a[1]),int(a[2]))
        return S

Finally, attach the detailed code:

def Append(S,s):
        for i in s:
                S.append(i)

def Insert(S,x,s):
        for i in range(len(s)):
                S.insert(x-1+i,s[i])

def Swap(S,a,b,c,d):
        n = len(S)
        if n > d:
                n1 = S[0:a-1]
                n2 = S[a-1:b]
                n3 = S[b:c-1]
                n4 = S[c-1:d]
                n5 = S[d:]
                S = n1+n4+n3+n2+n5
        elif n == d:
                n1 = S[0:a-1] 
                n2 = S[a-1:b]
                n3 = S[b:c-1]
                n4 = S[c-1:]
                S =  n1+n4+n3+n2
        return S

def Reverse(S,a,b):
        n = len(S)
        if a > 1:
                if b < n:
                        n1 = S[:a-1]
                        n2 = S[a-1:b]
                        n3 = S[b:]
                        S = n1 + n2[::-1] +n3
                elif b == n:
                        n1 = S[:a-1]
                        n2 = S[a-1:]
                        S = n1 + n2[::-1]
        else:
                if b < n:
                        n1 = S[:b]
                        n2 = S[b:]
                        s = n1[::-1] + n2
                else:
                        S = S[::-1]
        return S

def fun(S,nums):
        a = nums.split()
        if a[0] == 'Append':
                Append(S,a[1])
        elif a[0] == 'Insert':
                Insert(S,int(a[1]),a[2])
        elif a[0] == "Swap":
                S = Swap(S,int(a[1]),int(a[2]),int(a[3]),int(a[4]))
        elif a[0] == "Reverse":
                S = Reverse(S,int(a[1]),int(a[2]))
        return S

if __name__ == "__main__":
        S = list(input())
        n = int(input())
        if 1<=n<=150:
                while n > 0:
                        nums = input()
                        S = fun(S,nums)
                        n -= 1
        print(''.join(S))
Published 82 original articles · Like 11 · Visits 10,000+

Guess you like

Origin blog.csdn.net/Python_Matlab/article/details/105643247