Hướng dẫn c++ rotate string - C++ xoay chuỗi

Xem thảo luận

Show

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    1. Bàn luận
    2. Đưa ra một chuỗi kích thước n, viết các chức năng để thực hiện các hoạt động sau trên chuỗi.

    Examples:

    Input : s = "GeeksforGeeks"
            d = 2
    Output : Left Rotation  : "eksforGeeksGe" 
             Right Rotation : "ksGeeksforGee"  
    
    
    Input : s = "qwertyu" 
            d = 2
    Output : Left rotation : "ertyuqw"
             Right rotation : "yuqwert"

    Trái (hoặc ngược chiều kim đồng hồ) xoay chuỗi đã cho bằng các phần tử d (trong đó dWe have existing solution for this problem please refer Left Rotation and Right Rotation of a String link. We will solve this problem quickly in python using String Slicing. Approach is very simple,

    1. Phải (hoặc theo chiều kim đồng hồ) xoay chuỗi đã cho bằng các phần tử d (trong đó dfirst & second, for Left rotation Lfirst = str[0 : d] and Lsecond = str[d :]. For Right rotation Rfirst = str[0 : len(str)-d] and Rsecond = str[len(str)-d : ].
    2. Phương pháp 1: Chúng tôi có giải pháp hiện tại cho vấn đề này, vui lòng tham khảo xoay bên trái và xoay bên phải của một liên kết chuỗi. Chúng tôi sẽ giải quyết vấn đề này một cách nhanh chóng trong Python bằng cách sử dụng cắt chuỗi. Cách tiếp cận rất đơn giản,second + first accordingly.

    Implementation:

    Python3

    Chuỗi riêng biệt trong hai phần thứ nhất và thứ hai, cho xoay bên trái lfirst = str [0: d] và lsecond = str [d:]. Đối với vòng quay bên phải rfirst = str [0: len (str) -d] và rsecond = str [len (str) -d:].

    Bây giờ kết hợp hai phần này thứ hai + đầu tiên cho phù hợp.

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    3
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    4
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    5
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    6

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    8
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    5
    Left Rotation :  ksGeeksforGee
    Right Rotation :  ksGeeksforGee
    1
    Left Rotation :  ksGeeksforGee
    Right Rotation :  ksGeeksforGee
    2
    Left Rotation :  ksGeeksforGee
    Right Rotation :  ksGeeksforGee
    3

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    Left Rotation :  ksGeeksforGee
    Right Rotation :  ksGeeksforGee
    5
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    5
    Left Rotation :  ksGeeksforGee
    Right Rotation :  ksGeeksforGee
    8

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    03019
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    5
    Left Rotation :  ksGeeksforGee
    Right Rotation :  ksGeeksforGee
    1
    Left Rotation :  ksGeeksforGee
    Right Rotation :  ksGeeksforGee
    2
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    5
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    6
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    5
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    9
    from collections import deque
    
    items = deque('HELLO')
    items.rotate(1)
    
    ''.join(items)  # 'OHELL'
    
    0____41

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    from collections import deque
    
    items = deque('HELLO')
    items.rotate(1)
    
    ''.join(items)  # 'OHELL'
    
    3
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    5
    Left Rotation :  ksGeeksforGee
    Right Rotation :  ksGeeksforGee
    1
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    6
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    5
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    9
    from collections import deque
    
    items = deque('HELLO')
    items.rotate(1)
    
    ''.join(items)  # 'OHELL'
    
    0
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    2

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    4
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    55____56
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    7

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    4
    >>> s[-1]
    'O'
    
    0
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    6
    >>> s[-1]
    'O'
    
    2

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    >>> s[:-1]
    'HELL'
    
    3
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    >>> s[:-1]
    'HELL'
    
    5

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    4
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    5
    >>> s[:-1]
    'HELL'
    
    9

    Output:

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee

    >>> s[-1]
    'O'
    
    3
    >>> s[-1]
    'O'
    
    4
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    >>> s[-1]
    'O'
    
    7
    We use extended string to rotate the string. We will solve this problem quickly in python by slicing extended string. Approach is very simple,

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    5
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    >>> s[:-1]
    'HELL'
    
    1
    Now print this string. 

    Implementation:

    Python3

    Phương pháp 2: Chúng tôi sử dụng chuỗi mở rộng để xoay chuỗi. Chúng tôi sẽ giải quyết vấn đề này một cách nhanh chóng trong Python bằng cách cắt chuỗi mở rộng. Cách tiếp cận rất đơn giản,

    Sử dụng chuỗi mở rộng extend_str, cho vòng quay bên trái lfirst = extended_str [n: l1+n]. Đối với vòng quay bên phải rfirst = str [l1-n: l2-n] .now in chuỗi này. & Nbsp;

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    3
    def leftShift(text,n):
        return text[n:] + text[:n]
    
    1

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    def leftShift(text,n):
        return text[n:] + text[:n]
    
    3
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    def leftShift(text,n):
        return text[n:] + text[:n]
    
    5
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    6
    def leftShift(text,n):
        return text[n:] + text[:n]
    
    7

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    def leftShift(text,n):
        return text[n:] + text[:n]
    
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    6
    def rightShift(text,n):
        return text[-n:] + text[:-n]
    
    2

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    def rightShift(text,n):
        return text[-n:] + text[:-n]
    
    4
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    6
    def rightShift(text,n):
        return text[-n:] + text[:-n]
    
    7

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    8
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    01
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    6
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    03

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    8
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    07
    from collections import deque
    
    items = deque('HELLO')
    items.rotate(1)
    
    ''.join(items)  # 'OHELL'
    
    0
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    09____40
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    03

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    4
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    15
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    16

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    4
    >>> s[-1]
    'O'
    
    0
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    6
    >>> s[-1]
    'O'
    
    2

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    >>> s[:-1]
    'HELL'
    
    3
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    >>> s[:-1]
    'HELL'
    
    5

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    4
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    5
    >>> s[:-1]
    'HELL'
    
    9

    >>> s[-1]
    'O'
    
    3
    >>> s[-1]
    'O'
    
    4
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    >>> s[-1]
    'O'
    
    7

    Left Rotation :  ksGeeksforGee
    Right Rotation :  ksGeeksforGee

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    5
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    >>> s[:-1]
    'HELL'
    
    1

    Phương pháp 2: Chúng tôi sử dụng chuỗi mở rộng để xoay chuỗi. Chúng tôi sẽ giải quyết vấn đề này một cách nhanh chóng trong Python bằng cách cắt chuỗi mở rộng. Cách tiếp cận rất đơn giản,Feb 4, 2018 at 10:55

    2

    Sử dụng chuỗi mở rộng extend_str, cho vòng quay bên trái lfirst = extended_str [n: l1+n]. Đối với vòng quay bên phải rfirst = str [l1-n: l2-n] .now in chuỗi này. & Nbsp;

    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    3
    def leftShift(text,n):
        return text[n:] + text[:n]
    
    1

    from collections import deque
    
    items = deque('HELLO')
    items.rotate(1)
    
    ''.join(items)  # 'OHELL'
    

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    def leftShift(text,n):
        return text[n:] + text[:n]
    
    3
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    def leftShift(text,n):
        return text[n:] + text[:n]
    
    5
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    6
    def leftShift(text,n):
        return text[n:] + text[:n]
    
    7Feb 4, 2018 at 11:06

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    def leftShift(text,n):
        return text[n:] + text[:n]
    
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    6
    def rightShift(text,n):
        return text[-n:] + text[:-n]
    
    2jpp

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    def rightShift(text,n):
        return text[-n:] + text[:-n]
    
    4
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    6
    def rightShift(text,n):
        return text[-n:] + text[:-n]
    
    731 gold badges256 silver badges317 bronze badges

    1

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    8
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    01
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    6
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    03

    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    8
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    07
    from collections import deque
    
    items = deque('HELLO')
    items.rotate(1)
    
    ''.join(items)  # 'OHELL'
    
    0
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    09____40
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    03

    >>> s[-1]
    'O'
    

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    4
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    15
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    16

    >>> s[:-1]
    'HELL'
    

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    7
    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    4
    def rotate(strg, n):
        return strg[n:] + strg[:n]
    
    rotate('HELLO', -1)  # 'OHELL'
    
    7
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    20
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    21

    >>> s[-1]
    'O'
    
    3
    >>> s[-1]
    'O'
    
    4
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    9
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    26
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    27Feb 4, 2018 at 10:58

    Đầu raMike Müller

    Tôi đã cố gắng thực hiện chuỗi

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    40 thành
    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    41 trong Python. Nhưng không thể có được bất kỳ cách nào để xoay nó mà không làm việc với các vòng lặp. Làm thế nào để mã cho nó chỉ trong 1-2 dòng để tôi có thể nhận được mẫu mong muốn?18 gold badges157 silver badges159 bronze badges

    2

    Đã hỏi ngày 4 tháng 2 năm 2018 lúc 10:55

    Đây là một cách:

    def leftShift(text,n):
        return text[n:] + text[:n]
    

    Ngoài ra,

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    42 ("Hàng đợi hai lần") được tối ưu hóa cho các hoạt động liên quan đến hàng đợi. Nó có một phương thức xoay () chuyên dụng:

    def rightShift(text,n):
        return text[-n:] + text[:-n]
    

    Đã trả lời ngày 4 tháng 2 năm 2018 lúc 11:06Mar 18, 2020 at 23:50

    JPPJPPClayton C.

    151K31 Huy hiệu vàng256 Huy hiệu bạc317 Huy hiệu đồng1 gold badge8 silver badges16 bronze badges

    Bạn có thể cắt và thêm chuỗi:

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    0

    Điều này mang lại cho bạn nhân vật cuối cùng:Aug 30, 2019 at 14:28

    Và đây là tất cả mọi thứ nhưng cuối cùng:Konchog

    Cuối cùng, thêm chúng với

    >>> s = 'HELLO'
    >>> s[-1] + s[:-1]
    'OHELL'
    
    6.18 silver badges21 bronze badges

    Đã trả lời ngày 4 tháng 2 năm 2018 lúc 10:58

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    1

    Mike Müllermike Müller

    Left Rotation  : eksforGeeksGe 
    Right Rotation : ksGeeksforGee
    2

    79K18 Huy hiệu vàng157 Huy hiệu bạc159 Huy hiệu đồng

    Đây là những gì tôi sử dụng để xoay chuỗi trong Python3:Mar 16, 2019 at 20:59

    Để xoay bên trái bởi n: