检查字符串的字符是否可以通过替换“_”使其成为非递减的


在本文中,我们将深入探讨字符串操作领域一个有趣的问题:如何检查给定字符串的字符是否可以通过替换“?”字符使其成为非递减的。这个问题为磨练您在 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 次查看

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告