檢查字串的字元是否可以透過替換“_”使其成為非遞減的


在本文中,我們將深入探討字串操作領域一個有趣的問題:如何檢查給定字串的字元是否可以透過替換“?”字元使其成為非遞減的。這個問題為磨練您在 C++ 中的字串操作和條件檢查技能提供了絕佳的機會。

問題陳述

給定一個由字母字元和問號(?)組成的字串,確定這些字元是否可以透過替換“?”使其成為非遞減的。

非遞減條件意味著對於字串中的每兩個相鄰字元,第二個字元的 ASCII 值不小於第一個字元的 ASCII 值。

方法

我們將使用一種簡單的方法來解決這個問題:

  • 從左到右遍歷字串。

  • 如果遇到“?”,則將其替換為其前面的字元(除非它是第一個字元,在這種情況下將其替換為“a”)。

  • 最後,檢查結果字串是否為非遞減的。

示例

我們將使用一種簡單的方法來解決這個問題:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

// Function to check if a string can be made non-decreasing by replacing '?'s
bool checkNonDecreasing(char *s) {
   int n = strlen(s);
   
   // If the first character is '?', replace it with 'a'
   if (s[0] == '?')
      s[0] = 'a';
   
   // Iterate through the string
   for (int i = 1; i < n; i++) {
      // If the current character is '?', replace it with the previous character
      if (s[i] == '?')
         s[i] = s[i-1];
      
      // If the current character is less than the previous character, the string cannot be made non-decreasing
      if (s[i] < s[i-1])
         return false;
   }
   
   return true; // String can be made non-decreasing
}
int main() {
   char s[] = "ac?xy";
   bool result = checkNonDecreasing(s);
   
   // Print the result
   if (result)
      printf("Yes, the string can be made non-decreasing by replacing '?'s.\n");
   else
      printf("No, the string cannot be made non-decreasing by replacing '?'s.\n");
   
   return 0;
}

輸出

Yes, the string can be made non-decreasing by replacing '?'s.
#include<bits/stdc++.h>
using namespace std;

// Function to check if a string can be made non-decreasing by replacing '?'s
bool checkNonDecreasing(string s) {
   int n = s.size();
   // If the first character is '?', replace it with 'a'
   if (s[0] == '?') s[0] = 'a';
   // Iterate through the string
   for (int i = 1; i < n; i++) {
      // If the current character is '?', replace it with the previous character
      if (s[i] == '?') s[i] = s[i-1];
      // If the current character is less than the previous character, the string cannot be made non-decreasing
      if (s[i] < s[i-1]) return false;
   }
   return true;  // String can be made non-decreasing
}
int main() {
   string s = "ac?xy";
   bool result = checkNonDecreasing(s);
   // Print the result
   if(result)
      cout << "Yes, the string can be made non-decreasing by replacing '?'s.\n";
   else
      cout << "No, the string cannot be made non-decreasing by replacing '?'s.\n";
   return 0;
}

輸出

Yes, the string can be made non-decreasing by replacing '?'s.
public class Main {
   // Function to check if a string can be made non-decreasing by replacing '?'s
   public static boolean checkNonDecreasing(String s) {
      int n = s.length();
      char[] arr = s.toCharArray();

      // If the first character is '?', replace it with 'a'
      if (arr[0] == '?')
         arr[0] = 'a';

      // Iterate through the string
      for (int i = 1; i < n; i++) {
         // If the current character is '?', replace it with the previous character
         if (arr[i] == '?')
            arr[i] = arr[i-1];

         // If the current character is less than the previous character, the string cannot be made non-decreasing
         if (arr[i] < arr[i-1])
            return false;
      }

      return true; // String can be made non-decreasing
   }

   public static void main(String[] args) {
      String s = "ac?xy";
      boolean result = checkNonDecreasing(s);

      // Print the result
      if (result)
         System.out.println("Yes, the string can be made non-decreasing by replacing '?'s.");
      else
         System.out.println("No, the string cannot be made non-decreasing by replacing '?'s.");
   }
}

輸出

Yes, the string can be made non-decreasing by replacing '?'s.
# Function to check if a string can be made non-decreasing by replacing '?'s
def checkNonDecreasing(s):
   n = len(s)
   s = list(s)
   
   # If the first character is '?', replace it with 'a'
   if s[0] == '?':
      s[0] = 'a'
   
   # Iterate through the string
   for i in range(1, n):
      # If the current character is '?', replace it with the previous character
      if s[i] == '?':
         s[i] = s[i-1]
      
      # If the current character is less than the previous character, the string cannot be made non-decreasing
      if s[i] < s[i-1]:
         return False
   
   return True  # String can be made non-decreasing

s = "ac?xy"
result = checkNonDecreasing(s)

# Print the result
if result:
   print("Yes, the string can be made non-decreasing by replacing '?'s.")
else:
   print("No, the string cannot be made non-decreasing by replacing '?'s.")

輸出

Yes, the string can be made non-decreasing by replacing '?'s.

checkNonDecreasing 函式以字串 s 作為輸入,並返回一個布林值,指示字串的字元是否可以透過替換“?”使其成為非遞減的。

在此測試用例中,輸入字串為“ac?b”。checkNonDecreasing 函式以該字串作為引數被呼叫,結果是一個布林值,該值被打印出來。

結論

檢查字串的字元是否可以透過替換“?”使其成為非遞減的,這是一個測試您對字串操作和 ASCII 值理解的問題。透過練習此類問題,您可以增強您在 C++ 中的字串處理能力。

更新於: 2023年10月16日

191 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告