📜  查找与给定数组中的所有模式匹配的字符串

📅  最后修改于: 2021-09-04 07:59:40             🧑  作者: Mango

给定一个字符串数组arr[] ,其中包含字符模式,“*”表示任何字符集,包括空字符串。任务是找到一个匹配数组中所有模式的字符串。
注意:如果没有这种可能的模式,则打印 -1。
例子:

方法:思路是在所有模式中找到共同的前缀和后缀字符串,然后每个模式的中间可以替换为每个模式中的第一个或最后一个“*”。下面是该方法的说明:

  • 创建三个与每个模式的前缀、后缀和中间部分匹配的空字符串。
  • 迭代数组中的每个模式,对于每个模式:
    • 找到模式中的第一个和最后一个“*”。
    • 迭代现有前缀并检查它是否与模式的当前前缀匹配,如果任何字符与模式不匹配,则返回 -1。
    • 如果当前模式的前缀中有剩余部分,则将其附加到公共字符串的前缀。
    • 同样,匹配模式的后缀。
    • 最后,在普通字符串的中间初始化字符串附加字符串的所有中间字符,除了“*”。
  • 最后,串联共同字符串,它是前缀,中间和字符串的后缀部分的部分。

下面是上述方法的实现:

C++
// C++ implementation to find the
// string which matches
// all the patterns
 
#include
using namespace std;
 
// Function to find a common string
// which matches all the pattern
string find(vector S,int N)
{
    // For storing prefix till
    // first most * without conflicts
    string pref;
     
    // For storing suffix till
    // last most * without conflicts
    string suff;
     
    // For storing all middle
    // characters between
    // first and last *
    string mid;
         
    // Loop to iterate over every
    // pattern of the array
    for (int i = 0; i < N; i++) {
         
        // Index of the first "*"
        int first = int(
            S[i].find_first_of('*')
            );
            
        // Index of Last "*"
        int last = int(
            S[i].find_last_of('*')
            );
         
        // Iterate over the first "*"
        for (int z = 0; z < int(pref.size()) &&
                              z < first; z++) {
            if (pref[z] != S[i][z]) {
                return "*";
            }
        }
         
        // Prefix till first most *
        // without conflicts
        for (int z = int(pref.size());
                       z < first; z++) {
            pref += S[i][z];
        }
         
        // Iterate till last
        // most * from last
        for (int z = 0; z < int(suff.size()) &&
               int(S[i].size())-1-z > last; z++) {
            if (suff[z] != S[i][int(S[i].size())-1-z]) {
                return "*";
            }
        }
         
        // Make suffix till last
        // most * without conflicts
        for (int z = int(suff.size());
         int(S[i].size())-1-z > last; z++) {
            suff += S[i][int(S[i].size())-1-z];
        }
         
        // Take all middle characters
        // in between first and last most *
        for (int z = first; z <= last; z++) {
            if (S[i][z] != '*') mid += S[i][z];
        }
    }
     
    reverse(suff.begin(), suff.end());
    return pref + mid + suff;
}
 
// Driver Code
int main() {
 
    int N = 3;
    vector s(N);
     
    // Take all
    // the strings
    s[0]="pq*du*q";
    s[1]="pq*abc*q";
    s[2]="p*d*q";
     
    // Method for finding
    // common string
    cout<


Python3
# Python3 implementation
# to find the string which
# matches all the patterns
 
# Function to find a common
# string which matches all
# the pattern
def find(S, N):
 
    # For storing prefix
    # till first most *
    # without conflicts
    pref = ""
 
    # For storing suffix
    # till last most *
    # without conflicts
    suff = ""
 
    # For storing all middle
    # characters between
    # first and last *
    mid = ""
 
    # Loop to iterate over every
    # pattern of the array
    for i in range(N):
 
        # Index of the first "*"
        first = int(S[i].index("*"))
 
        # Index of Last "*"
        last = int(S[i].rindex("*"))
 
        # Iterate over the first "*"
        for z in range(len(pref)):
            if(z < first):
                if(pref[z] != S[i][z]):
                    return "*"
 
        # Prefix till first most *
        # without conflicts
        for z in range(len(pref),first):
            pref += S[i][z];
 
        # Iterate till last
        # most * from last
        for z in range(len(suff)):
            if(len(S[i]) - 1 - z > last):
                if(suff[z] != S[i][len(S[i]) - 1 - z]):
                    return "*"
 
        # Make suffix till last
        # most * without conflicts
        for z in range(len(suff),
                       len(S[i]) - 1 - last):
            suff += S[i][len(S[i]) - 1 - z]
 
        # Take all middle characters
        # in between first and last most *
        for z in range(first, last + 1):
            if(S[i][z] != '*'):
                mid += S[i][z]
     
    suff=suff[:: -1]
    return pref + mid + suff
 
# Driver Code
N = 3
s = ["" for i in range(N)]
 
# Take all
# the strings
s[0] = "pq*du*q"
s[1] = "pq*abc*q"
s[2] = "p*d*q"
 
# Method for finding
# common string
print(find(s, N))
 
# This code is contributed by avanitrachhadiya2155


输出:
pqduabcdq

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live