📜  删除禁止的字符串

📅  最后修改于: 2021-06-25 21:24:22             🧑  作者: Mango

给定一个字符串W,以这样的方式改变字符串,它不包含任何“禁止入内”的字符串S1与Sn作为其子之一。控制此更改的规则如下:

1.字符大小写无关紧要,即“ XyZ”与“ xYZ”相同。
2.更改原始字符串W中子字符串覆盖的所有字符,以使特定字母lt在更改后的字符串出现的次数最多,并且从字法上来讲,更改后的字符串是可以从所有可能的组合中获得的最小字符串。
3.不在禁止的子字符串中的字符不允许更改。
4.更改的字符必须与字符串W中该位置的原始字符的大小写相同。
5.如果字母lt是子字符串的一部分,则必须根据上述规则将其更改为其他字符。

例子:

Input : n = 3  
        s1 = "etr"
        s2 = "ed" 
        s3 = "ied"
        W = "PEtrUnited"
        letter = "d"
Output : PDddUnitda

Input : n = 1
        s1 = "PetrsDreamOh"
        W = "PetrsDreamOh"
        letter = h
Output : HhhhhHhhhhHa

解释:
示例1:第一个字符P不属于任何子字符串,因此不会更改。接下来的三个字符形成子字符串“ etr”,并更改为“ Ddd”。接下来的四个字符不属于任何禁止的子字符串,并且保持不变。接下来的两个字符形成子字符串“ ed”,并更改为“ da”,因为d是最后一个字符本身,所以用另一个字符“ a”代替了该字符,从而使该字符串在字典上最小。
注意: “ Etr” =“ etr”,更改后的子字符串“ Ddd”的第一个字符为“ D”,因为“ Etr”的首字母大写。

示例2:由于整个字符串都是禁止的字符串,因此根据情况,从第一个到倒数第二个字符将其替换为字母’h’。最后一个字符为“ h”,因此将其替换为字母“ a”,以使该字符串在字典上最小。

方法:
检查字符串W的每个字符(是否为任何子字符串的开头)。使用另一个数组来记录W字符,这些字符是禁止的字符串的一部分,需要更改。
字符根据以下规则进行更改:
1.如果W [i] =字母且字母=’a’,则W [i] =’b’
2.如果W [i] =字母和字母!=’a’,则W [i] =’a’
3.如果W [i]!=字母,则W [i] =字母

当W [i]是大写字符时,也将使用第一个条件和第二个条件。

下面是上述方法的实现:

C++
// CPP program to remove the forbidden strings
#include 
using namespace std;
  
// pre[] keeps record of the characters
// of w that need to be changed
bool pre[100];
  
// number of forbidden strings
int n;
  
// given string
string w;
  
// stores the forbidden strings
string s[110];
  
// letter to replace and occur
// max number of times
char letter;
  
// Function to check if the particula
// r substring is present in w at position
void verify(int position, int index)
{
    int l = w.length();
    int k = s[index].length();
      
    // If length of substring from this
    // position is greater than length
    // of w then return
    if (position + k > l)
        return;
      
    bool same = true;
    for (int i = position; i < position + k;
                                      i++) {
      
        // n and n1 are used to check for 
        // substring without considering
        // the case of the letters in w
        // by comparing the difference 
        // of ASCII values
        int n, n1;
        char ch = w[i];
        char ch1 = s[index][i - position];
        if (ch >= 'a' && ch <= 'z')
            n = ch - 'a';
        else
            n = ch - 'A';
        if (ch1 >= 'a' && ch1 <= 'z')
            n1 = ch1 - 'a';
        else
            n1 = ch1 - 'A';
        if (n != n1)
            same = false;
    }
      
    // If same == true then it means a 
    // substring was found starting at
    // position therefore all characters 
    // from position to length of substring
    // found need to be changed therfore
    // they needs to be marked
    if (same == true) {
        for (int i = position; i < position + k;
                                           i++)
            pre[i] = true;
        return;
    }
}
  
// Function implementing logic
void solve()
{
    int l = w.length();
    int p = letter - 'a';
  
    for (int i = 0; i < 100; i++)
        pre[i] = false;
  
    // To verify if any substring is
    // starting from index i
    for (int i = 0; i < l; i++) {
        for (int j = 0; j < n; j++)
            verify(i, j);
    }
      
    // Modifying the string w 
    // according to th rules
    for (int i = 0; i < l; i++) {
        if (pre[i] == true) {
            if (w[i] == letter)
                w[i] = (letter == 'a') ? 'b' : 'a';
      
            // This condition checks 
            // if w[i]=upper(letter)
            else if (w[i] == 'A' + p)
                w[i] = (letter == 'a') ? 'B' : 'A';
              
            // This condition checks if w[i]
            // is any lowercase letter apart
            // from letter. If true replace
            // it with letter
            else if (w[i] >= 'a' && w[i] <= 'z')
                w[i] = letter;
              
            // This condition checks if w[i]
            // is any uppercase letter apart 
            // from letter. If true then 
            // replace it with upper(letter).
            else if (w[i] >= 'A' && w[i] <= 'Z')
                w[i] = 'A' + p;
        }
    }
    cout << w;
}
  
// Driver function for the program
int main()
{
    n = 3;
    s[0] = "etr";
    s[1] = "ed";
    s[2] = "ied";
    w = "PEtrUnited";
    letter = 'd';
  
    // Calling function
    solve();
    return 0;
}


Java
// Java program to remove forbidden strings 
import java.io.*;
  
class rtf {
      
    // number of forbidden strings
    public static int n;
      
    // original string
    public static String z;
  
    // forbidden strings
    public static String s[] = new String[100];
  
    // to store original string 
    // as character array
    public static char w[];
      
    // letter to replace and occur
    // max number of times
    public static char letter;   
      
    // pre[] keeeps record of the charcters
    // of w that need to be changed
    public static boolean pre[] = new boolean[100];
  
    // Function to check if the particular
    // substring is present in w at position
    public static void verify(int position, int index)
    {
        int l = z.length();
        int k = s[index].length();
      
        // If length of substring from this 
        // position is greater than length
        // of w then return
        if (position + k > l)
            return;
          
        boolean same = true;
        for (int i = position; i < position + k; i++) {
          
            // n and n1 are used to check for 
            // substring without considering
            // the case of the letters in w 
            // by comparing the difference
            // of ASCII values
            int n, n1;
            char ch = w[i];
            char ch1 = s[index].charAt(i - position);
            if (ch >= 'a' && ch <= 'z')
                n = ch - 'a';
            else
                n = ch - 'A';
            if (ch1 >= 'a' && ch1 <= 'z')
                n1 = ch1 - 'a';
            else
                n1 = ch1 - 'A';
            if (n != n1)
                same = false;
        }
          
        // If same == true then it means a substring
        // was found starting at position therefore
        // all characters from position to length 
        // of substring found need to be changed 
        // therfore they need to be marked
        if (same == true) {
            for (int i = position; i < position + k; 
                                               i++)
                pre[i] = true;
            return;
        }
    }
      
    // Function performing calculations.
    public static void solve()
    {
        w = z.toCharArray();
        letter = 'd';
        int l = z.length();
        int p = letter - 'a';
        for (int i = 0; i < 100; i++)
            pre[i] = false;
      
        // To verify if any substring is
        // starting from index i
        for (int i = 0; i < l; i++) {
            for (int j = 0; j < n; j++)
                verify(i, j);
        }
      
        // Modifying the string w 
        // according to th rules
        for (int i = 0; i < l; i++) {
            if (pre[i] == true) {
                if (w[i] == letter)
                    w[i] = (letter == 'a') ? 'b' : 'a';
      
                // This condition checks 
                // if w[i]=upper(letter)
                else if (w[i] == (char)((int)'A' + p))
                    w[i] = (letter == 'a') ? 'B' : 'A';
      
                // This condition checks if w[i] 
                // is any lowercase letter apart 
                // from letter. If true replace
                // it with letter
                else if (w[i] >= 'a' && w[i] <= 'z')
                    w[i] = letter;
      
                // This condition checks if w[i] 
                // is any uppercase letter apart
                // from letter. If true then 
                // replace it with upper(letter).
                else if (w[i] >= 'A' && w[i] <= 'Z')
                    w[i] = (char)((int)'A' + p);
            }
        }
        System.out.println(w);
    }
      
    // Driver function for the program
    public static void main(String args[])
    {
        n = 3;
        s[0] = "etr";
        s[1] = "ed";
        s[2] = "ied";
        z = "PEtrUnited";
        solve();
    }
}


Python3
# Python program to remove the forbidden strings
  
# pre[] keeps record of the characters
# of w that need to be changed
pre = [False]*100
  
# stores the forbidden strings
s = [None]*110
  
# Function to check if the particula
# r substring is present in w at position
def verify(position, index):
    l = len(w)
    k = len(s[index])
      
    # If length of substring from this
    # position is greater than length
    # of w then return
    if (position + k > l):
        return
      
    same = True
    for i in range(position, position + k):
        # n and n1 are used to check for 
        # substring without considering
        # the case of the letters in w
        # by comparing the difference 
        # of ASCII values
        ch = w[i]
        ch1 = s[index][i - position]
        if (ch >= 'a' and ch <= 'z'):
            n = ord(ch) - ord('a')
        else:
            n = ord(ch) - ord('A')
        if (ch1 >= 'a' and ch1 <= 'z'):
            n1 = ord(ch1) - ord('a')
        else:
            n1 = ord(ch1) - ord('A')
        if (n != n1):
            same = False
      
      
    # If same == true then it means a 
    # substring was found starting at
    # position therefore all characters 
    # from position to length of substring
    # found need to be changed therfore
    # they needs to be marked
    if (same == True):
        for i in range( position, position + k):
            pre[i] = True
        return
  
# Function implementing logic
def solve():
  
    l = len(w)
    p = ord(letter) - ord('a')
  
    # To verify if any substring is
    # starting from index i
    for i in range(l):
        for j in range(n):
            verify(i, j)
      
    # Modifying the string w 
    # according to th rules
    for i in range(l):
        if (pre[i] == True):
            if (w[i] == letter):
                w[i] = 'b' if (letter == 'a') else 'a'
      
            # This condition checks 
            # if w[i]=upper(letter)
            elif (w[i] == str(ord('A') + p)):
                w[i] = 'B' if (letter == 'a') else 'A'
              
            # This condition checks if w[i]
            # is any lowercase letter apart
            # from letter. If true replace
            # it with letter
            elif (w[i] >= 'a' and w[i] <= 'z'):
                w[i] = letter
              
            # This condition checks if w[i]
            # is any uppercase letter apart 
            # from letter. If true then 
            # replace it with upper(letter).
            elif (w[i] >= 'A' and w[i] <= 'Z'):
                w[i] = chr(ord('A') + p)
          
    print(''.join(w))
  
# Driver function for the program
  
# number of forbidden strings
n = 3
  
s[0] = "etr"
s[1] = "ed"
s[2] = "ied"
  
# given string
w = "PEtrUnited"
w = list(w)
  
# letter to replace and occur
# max number of times
letter = 'd'
  
# Calling function
solve()
  
  
# This code is contributed by ankush_953


C#
// C# program to remove forbidden strings 
using System;
  
class GFG
{
  
// number of forbidden strings 
public static int n;
  
// original string 
public static string z;
  
// forbidden strings 
public static string[] s = new string[100];
  
// to store original string 
// as character array 
public static char[] w;
  
// letter to replace and occur 
// max number of times 
public static char letter;
  
// pre[] keeeps record of the charcters 
// of w that need to be changed 
public static bool[] pre = new bool[100];
  
// Function to check if the particular 
// substring is present in w at position 
public static void verify(int position,
                          int index)
{
    int l = z.Length;
    int k = s[index].Length;
  
    // If length of substring from this 
    // position is greater than length 
    // of w then return 
    if (position + k > l)
    {
        return;
    }
  
    bool same = true;
    for (int i = position;
             i < position + k; i++)
    {
  
        // n and n1 are used to check for 
        // substring without considering 
        // the case of the letters in w 
        // by comparing the difference 
        // of ASCII values 
        int n, n1;
        char ch = w[i];
        char ch1 = s[index][i - position];
        if (ch >= 'a' && ch <= 'z')
        {
            n = ch - 'a';
        }
        else
        {
            n = ch - 'A';
        }
        if (ch1 >= 'a' && ch1 <= 'z')
        {
            n1 = ch1 - 'a';
        }
        else
        {
            n1 = ch1 - 'A';
        }
        if (n != n1)
        {
            same = false;
        }
    }
  
    // If same == true then it means a 
    // substring was found starting at 
    // position therefore all characters 
    // from position to length of substring
    // found need to be changed therfore
    // they need to be marked 
    if (same == true)
    {
        for (int i = position; 
                 i < position + k; i++)
        {
            pre[i] = true;
        }
        return;
    }
}
  
// Function performing calculations. 
public static void solve()
{
    w = z.ToCharArray();
    letter = 'd';
    int l = z.Length;
    int p = letter - 'a';
    for (int i = 0; i < 100; i++)
    {
        pre[i] = false;
    }
  
    // To verify if any substring is 
    // starting from index i 
    for (int i = 0; i < l; i++)
    {
        for (int j = 0; j < n; j++)
        {
            verify(i, j);
        }
    }
  
    // Modifying the string w 
    // according to th rules 
    for (int i = 0; i < l; i++)
    {
        if (pre[i] == true)
        {
            if (w[i] == letter)
            {
                w[i] = (letter == 'a') ? 'b' : 'a';
            }
  
            // This condition checks 
            // if w[i]=upper(letter) 
            else if (w[i] == (char)((int)'A' + p))
            {
                w[i] = (letter == 'a') ? 'B' : 'A';
            }
  
            // This condition checks if w[i] 
            // is any lowercase letter apart 
            // from letter. If true replace 
            // it with letter 
            else if (w[i] >= 'a' && w[i] <= 'z')
            {
                w[i] = letter;
            }
  
            // This condition checks if w[i] 
            // is any uppercase letter apart 
            // from letter. If true then 
            // replace it with upper(letter). 
            else if (w[i] >= 'A' && w[i] <= 'Z')
            {
                w[i] = (char)((int)'A' + p);
            }
        }
    }
    Console.WriteLine(w);
}
  
// Driver Code 
public static void Main(string[] args)
{
    n = 3;
    s[0] = "etr";
    s[1] = "ed";
    s[2] = "ied";
    z = "PEtrUnited";
    solve();
}
}
  
// This code is contributed by Shrikanth13


输出:

PDddUnitda

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。