📌  相关文章
📜  就地替换模式的多次出现

📅  最后修改于: 2021-04-24 16:11:16             🧑  作者: Mango

给定一个字符串和一个模式,用字符“ X”替换多次出现的模式。转换应就地进行,解决方案应使用单个’X’替换模式的多个连续(且不重叠)的出现

String – GeeksForGeeks
Pattern – Geeks
Output: XforX
 
String – GeeksGeeks
Pattern – Geeks
Output: X

String – aaaa
Pattern – aa
Output: X

String – aaaaa
Pattern – aa
Output: Xa

想法是维护两个索引i和j进行就地替换。索引i始终指向输出字符串中的下一个字符。索引j遍历字符串并搜索一个或多个模式匹配。如果找到匹配项,则将字符“ X”放在索引i处,并将索引i递增1,将索引j递增模式的长度。如果我们发现模式连续出现多次,索引i只会增加一次。如果找不到该模式,则将当前字符从索引j复制到索引i,并将i和j都增加1。由于模式长度始终大于等于1,并且替换长度仅为1个字符,因此我们永远不会覆盖未处理的字符即j> = i是不变的。

// C++ program to in-place replace multiple
// occurrences of a pattern by character ‘X’
#include 
using namespace std;
  
// returns true if pattern is prefix of str
bool compare(char* str, char* pattern)
{
    for (int i = 0; pattern[i]; i++)
        if (str[i] != pattern[i])
            return false;
    return true;
}
  
// Function to in-place replace multiple
// occurrences of a pattern by character ‘X’
void replacePattern(char* str, char* pattern)
{
    // If pattern is null or empty string,
    // nothing needs to be done
    if (pattern == NULL)
        return;
  
    int len = strlen(pattern);
    if (len == 0)
        return;
  
    int i = 0, j = 0;
    int count;
  
    // for each character
    while (str[j]) {
        count = 0;
  
        // compare str[j..j+len] with pattern
        while (compare(str + j, pattern)) {
            // increment j by length of pattern
            j = j + len;
            count++;
        }
  
        // If single or multiple occurrences of pattern
        // is found, replace it by character 'X'
        if (count > 0)
            str[i++] = 'X';
  
        // copy character at current position j
        // to position i and increment i and j
        if (str[j])
            str[i++] = str[j++];
    }
  
    // add a null character to terminate string
    str[i] = '\0';
}
  
// Driver code
int main()
{
    char str[] = "GeeksforGeeks";
    char pattern[] = "Geeks";
  
    replacePattern(str, pattern);
    cout << str;
  
    return 0;
}
输出:
XforX

上述算法的时间复杂度为O(n * m),其中n为字符串长度,m为模式长度。

使用STL实施

The idea of this implementation is to use the STL in-built functions 
to search for pattern string in main string and then erasing it 
from the main string
// C++ program to in-place replace multiple
// occurrences of a pattern by character ‘X’
#include 
using namespace std;
  
// Function to in-place replace multiple
// occurrences of a pattern by character ‘X’
void replacePattern(string str, string pattern)
{
  
    // making an iterator for string str
    string::iterator it = str.begin();
    // run this loop until iterator reaches end of string
    while (it != str.end()) {
        // searching the first index in string str where
        // the first occurrence of string pattern occurs
        it = search(str.begin(), str.end(), pattern.begin(), pattern.end());
        // checking if iterator is not pointing to end of the
        // string str
        if (it != str.end()) {
            // erasing the full pattern string from that iterator
            // position in string str
            str.erase(it, it + pattern.size());
            // inserting 'X' at that iterator posiion
            str.insert(it, 'X');
        }
    }
  
    // this loop removes consecutive 'X' in string s
    // Example: GeeksGeeksforGeeks was changed to 'XXforX'
    // running this loop will change it to 'XforX'
    for (int i = 0; i < str.size() - 1; i++) {
        if (str[i] == 'X' && str[i + 1] == 'X') {
            // removing 'X' at posiion i in string str
            str.erase(str.begin() + i);
            i--; // i-- because one character was deleted
            // so repositioning i
        }
    }
    cout << str;
}
  
// Driver code
int main()
{
    string str = "GeeksforGeeks";
    string pattern = "Geeks";
  
    replacePattern(str, pattern);
  
    return 0;
}
输出:
XforX