📌  相关文章
📜  查找出现在从 S 到 T 的按字典顺序递增的字符串序列中间的字符串

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

查找出现在从 S 到 T 的按字典顺序递增的字符串序列中间的字符串

给定两个字符串STS在字典上大于T ,任务是生成从ST包括两者)按字典顺序递增的字符串序列,并打印出现在序列中间的字符串。
注意:按字典顺序递增的序列中总是会有奇数个字符串。

例子:

解决方法:按照以下步骤解决问题:

  • 每个字符串都可以表示为 以 [0, 26) 之间的整数表示以 26 为基数。
  • 如果字符串表示两个整数LR ,那么结果将是 (L + R)/2 ,这将是中间数。
  • 使用类似的概念,字符串可以用以 26 为基数的数字来表示
  • 诸如“az”之类的字符串可以表示为(0 25) 26 ,“bf”可以表示为(1 5) 26 ;而“”可以表示为 () 26
  • 将字符串转换为各自的以 26 为基数的数字后,获得它们的按位求和
  • 添加从右到左迭代的位,并将余数带到下一个位置。
  • (0 25) 26和 (1 5) 26相加将是 (2 4) 26
  • 取每个位置值的中间并打印相应的字符。如果位置是奇数,则将下一个位置移动 26 个字符。

下面是上述方法的实现:

C++
// C++ Program for the above approach
 
#include 
using namespace std;
 
// Function to print the string at
// the middle of lexicographically
// increasing sequence of strings from S to T
int printMiddleString(string S, string T, int N)
{
    // Stores the base 26 digits after addition
    vector a1(N + 1);
 
    for (int i = 0; i < N; i++) {
        a1[i + 1] = S[i] - 'a' + T[i] - 'a';
    }
 
    // Iterete from right to left
    // and add carry to next position
    for (int i = N; i >= 1; i--) {
        a1[i - 1] += a1[i] / 26;
        a1[i] %= 26;
    }
 
    // Reduce the number to find the middle
    // string by dividing each position by 2
    for (int i = 0; i <= N; i++) {
 
        // If current value is odd,
        // carry 26 to the next index value
        if (a1[i] & 1) {
 
            if (i + 1 <= N) {
                a1[i + 1] += 26;
            }
        }
 
        a1[i] /= 2;
    }
 
    for (int i = 1; i <= N; i++) {
        cout << char(a1[i] + 'a');
    }
    return 0;
}
 
// Driver Code
int main()
{
    int N = 5;
    string S = "afogk";
    string T = "asdji";
    printMiddleString(S, T, N);
}


Java
// Java Program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to print the string at
    // the middle of lexicographically
    // increasing sequence of strings from S to T
    static void printMiddleString(String S, String T, int N)
    {
        // Stores the base 26 digits after addition
        int[] a1 = new int[N + 1];
 
        for (int i = 0; i < N; i++) {
            a1[i + 1] = (int)S.charAt(i) - 97
                        + (int)T.charAt(i) - 97;
        }
 
        // Iterete from right to left
        // and add carry to next position
        for (int i = N; i >= 1; i--) {
            a1[i - 1] += (int)a1[i] / 26;
            a1[i] %= 26;
        }
 
        // Reduce the number to find the middle
        // string by dividing each position by 2
        for (int i = 0; i <= N; i++) {
 
            // If current value is odd,
            // carry 26 to the next index value
            if ((a1[i] & 1) != 0) {
 
                if (i + 1 <= N) {
                    a1[i + 1] += 26;
                }
            }
 
            a1[i] = (int)a1[i] / 2;
        }
 
        for (int i = 1; i <= N; i++) {
            System.out.print((char)(a1[i] + 97));
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 5;
        String S = "afogk";
        String T = "asdji";
        printMiddleString(S, T, N);
    }
}
 
// This code is contributed by ukasp.


Python3
# Python Program for the above approach
 
# Function to print the string at
# the middle of lexicographically
# increasing sequence of strings from S to T
def printMiddleString(S, T, N):
   
  # Stores the base 26 digits after addition
  a1 = [0] * (N + 1);
 
  for i in range(N):
    a1[i + 1] = ord(S[i]) - ord("a") + ord(T[i]) - ord("a");
   
 
  # Iterete from right to left
  # and add carry to next position
  for i in range(N, 1, -1):
    a1[i - 1] += a1[i] // 26;
    a1[i] %= 26;
   
 
  # Reduce the number to find the middle
  # string by dividing each position by 2
  for i in range(N+1):
    # If current value is odd,
    # carry 26 to the next index value
    if (a1[i] & 1):
      if (i + 1 <= N):
        a1[i + 1] += 26;
   
    a1[i] = a1[i] // 2;
   
  for i in range(1, N + 1):
    print(chr(a1[i] + ord("a")), end="");
   
  return 0;
 
# Driver Code
N = 5;
S = "afogk";
T = "asdji";
printMiddleString(S, T, N);
 
# This code is contributed by gfgking


C#
// C# Program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to print the string at
// the middle of lexicographically
// increasing sequence of strings from S to T
static void printMiddleString(string S, string T, int N)
{
    // Stores the base 26 digits after addition
    int []a1 = new int[N + 1];
 
    for (int i = 0; i < N; i++) {
        a1[i + 1] = (int)S[i] - 97 + (int)T[i] - 97;
    }
 
    // Iterete from right to left
    // and add carry to next position
    for (int i = N; i >= 1; i--) {
        a1[i - 1] += (int)a1[i] / 26;
        a1[i] %= 26;
    }
 
    // Reduce the number to find the middle
    // string by dividing each position by 2
    for (int i = 0; i <= N; i++) {
 
        // If current value is odd,
        // carry 26 to the next index value
        if ((a1[i] & 1)!=0) {
 
            if (i + 1 <= N) {
                a1[i + 1] += 26;
            }
        }
 
        a1[i] = (int)a1[i]/2;
    }
 
    for (int i = 1; i <= N; i++) {
        Console.Write(Convert.ToChar(a1[i] + 'a'));
    }
     
}
 
// Driver Code
public static void Main()
{
    int N = 5;
    string S = "afogk";
    string T = "asdji";
    printMiddleString(S, T, N);
}
}
 
// This code is contributed by ipg2016107.


Javascript



输出:
alvuw

时间复杂度: O(N)
辅助空间: O(N)