📌  相关文章
📜  按给定长度移动所有前缀

📅  最后修改于: 2022-05-13 01:57:07.232000             🧑  作者: Mango

按给定长度移动所有前缀

给定一个包含字母和数字的字符串S和一个整数数组Shift其中, 1 \leq S.length()=length(Shift) \leq 10^{5}并且对于 Shift 数组的每个元素0 \leq Shift[i] \leq 10^{9} .任务是,对于每个Shift[i] = X ,您必须将S的前i+1个字母移动X 次。在将所有此类移位应用于S之后返回最终字符串。
注意:移位表示循环递增 ASCII 值。
例子:

方法: S 的第 i 个字符被移位Shift[i] + Shift[i+1] + … + Shift[Shift.length – 1] 次
因此,我们向后更新Shift数组,以知道要应用于字符串S的每个元素的确切移位次数。
现在,

C++
// CPP implementation of above approach
#include 
using namespace std;
  
// Function to find S after shifting each letter
string shift_S(string S, int Shift[], int n)
{
    // update shift array for each element
    for (int i = n - 2; i >= 0; --i)
        Shift[i] += Shift[i + 1];
  
    // finding the new shifted string
    string result = "";
  
    // traverse S and shift letters according to shift array
    for (int i = 0; i < S.length(); i++) {
  
        // For upper letters
        if (isupper(S[i])) {
            result += char((int(S[i]) + Shift[i] - 'A') % 26 + 'A');
        }
  
        // For lower letters
        else if (islower(S[i])) {            
            result += char((int(S[i]) + Shift[i] - 'a') % 26 + 'a');
        }
  
        // For digits
        else {            
            result += char((int(S[i]) + Shift[i] - '0') % 10 + '0');
        }
    }
  
    // Return the shifted string
    return result;
}
  
// Driver program
int main()
{
    string S = "abc";
    int Shift[] = { 2, 5, 9 };
  
    int n = sizeof(Shift) / sizeof(Shift[0]);
  
    // function call to print required answer
    cout << shift_S(S, Shift, n);
  
    return 0;
}
  
// This code is written by Sanjit_Prasad


Java
// Java implementation of the above approach 
  
public class GfG{
          
    // Function to find S after shifting each letter 
    public static String shift_S(String S, int Shift[], int n) 
    { 
        // update shift array for each element 
        for (int i = n - 2; i >= 0; --i) 
            Shift[i] += Shift[i + 1]; 
        
        // finding the new shifted string 
        String result = ""; 
        
        // traverse S and shift letters according to shift array 
        for (int i = 0; i < S.length(); i++) { 
        
            // For upper letters 
            if (Character.isUpperCase(S.charAt(i))) { 
                result += (char)(((int)(S.charAt(i)) + Shift[i] - 'A') % 26 + 'A'); 
            } 
        
            // For lower letters 
            else if (Character.isLowerCase(S.charAt(i))) {             
                result += (char)(((int)(S.charAt(i)) + Shift[i] - 'a') % 26 + 'a'); 
            } 
        
            // For digits 
            else {             
                result += (char)(((int)(S.charAt(i)) + Shift[i] - '0') % 10 + '0'); 
            } 
        } 
        
        // Return the shifted string 
        return result; 
    } 
  
     public static void main(String []args){
          
        String S = "abc"; 
        int Shift[] = { 2, 5, 9 }; 
        int n = Shift.length;
          
        // Function call to print the required answer 
        System.out.println(shift_S(S, Shift, n));
     }
}
    
// This code is contributed by Rituraj Jain


Python3
# Python3 implementation of above approach
  
# Function to find S after shifting
# each letter
def shift_S(S, Shift, n):
      
    # update shift array for 
    # each element
    for i in range(n - 2, -1, -1):
        Shift[i] = Shift[i] + Shift[i + 1]
      
    # finding the new shifted string
    result = ""
      
    # traverse S and shift letters
    # according to shift array
    for i in range(len(S)):
          
        # For upper letters
        if(S[i].isupper()):
            result = result + chr((ord(S[i]) + Shift[i] - 
                                   ord('A')) % 26 + ord('A'))
              
        # For lower letters
        elif (S[i].islower()):
            result = result + chr((ord(S[i]) + Shift[i] - 
                                   ord('a')) % 26 + ord('a'))
          
        # For digits
        else:
            result = result + chr((ord(S[i]) + Shift[i] - 
                                   ord('0')) % 10 + ord('0'))
          
    # Return the shifted string
    return result 
  
# Driver Code
S = "abc"
Shift = [2, 5, 9]
n = len(Shift)
  
# Function call to print the required answer
print(shift_S(S, Shift, n))
  
# This code is contributed 
# by Shashank_Sharma


C#
// C# implementation of the above approach 
using System;
  
class GfG{ 
          
// Function to find S after 
// shifting each letter 
public static String shift_S(string S, 
                             int[] Shift,
                             int n) 
{ 
      
    // Update shift array for each element 
    for(int i = n - 2; i >= 0; --i) 
        Shift[i] += Shift[i + 1]; 
      
    // Finding the new shifted string 
    string result = ""; 
      
    // Traverse S and shift letters 
    // according to shift array 
    for(int i = 0; i < S.Length; i++) 
    { 
          
        // For upper letters 
        if (Char.IsUpper(S[i])) 
        { 
            result += (char)(((int)(S[i]) + 
                     Shift[i] - 'A') % 26 + 'A'); 
        } 
      
        // For lower letters 
        else if (Char.IsLower(S[i]))
        {             
            result += (char)(((int)(S[i]) + 
                     Shift[i] - 'a') % 26 + 'a'); 
        } 
      
        // For digits 
        else 
        {             
            result += (char)(((int)(S[i]) + 
                     Shift[i] - '0') % 10 + '0'); 
        } 
    } 
      
    // Return the shifted string 
    return result; 
} 
  
// Driver code
public static void Main()
{ 
    string S = "abc"; 
    int[] Shift = { 2, 5, 9 }; 
    int n = Shift.Length; 
      
    // Function call to print 
    // the required answer 
    Console.WriteLine(shift_S(S, Shift, n)); 
} 
}
  
// This code is contributed by sanjoy_62


输出:
qpl

时间复杂度: O(N),其中 N 是字符串S 的长度。