给定两个字符串,其中一个是模式(模式),另一个是搜索表达式。搜索表达式包含“#”。
#以下列方式工作:
- #与一个或多个字符匹配。
- #会在找到模式匹配项之前匹配所有字符。例如,如果pat =“ A#B”,且文本为“ ACCBB”,则#仅与“ CC”匹配,并且认为未找到模式。
例子 :
Input : str = "ABABABA"
pat = "A#B#A"
Output : yes
Input : str = "ABCCB"
pat = "A#B"
Output : yes
Input : str = "ABCABCCE"
pat = "A#C#"
Output : yes
Input : str = "ABCABCCE"
pat = "A#C"
Output : no
我们可以观察到,每当遇到’#’时,我们都必须考虑尽可能多的字符,直到模式的下一个字符不等于给定字符串的当前字符。首先,我们检查模式的当前字符是否等于’#’-
a)如果不是,那么我们检查字符串和模式的当前字符是否相同,如果相同,则增加两个计数器,否则仅从此处返回false。无需进一步检查。
B)如果是的话,那么我们就必须找到在文本中字符的位置,随着该模式的下一个字符匹配。
C++
// C++ program for pattern matching
// where a single special character
// can match one more characters
#include
using namespace std;
// Returns true if pat matches with text
int regexMatch(string text, string pat)
{
int lenText = text.length();
int letPat = pat.length();
// i is used as an index in pattern
// and j as an index in text
int i = 0, j = 0;
// Traverse through pattern
while (i < letPat)
{
// If current character of
// pattern is not '#'
if (pat[i] != '#')
{
// If does not match with text
if (pat[i] != text[j])
return false;
// If matches, increment i and j
i++;
j++;
}
// Current character is '#'
else
{
// At least one character
// must match with #
j++;
// Match characters with # until
// a matching character is found.
while (text[j] != pat[i + 1])
j++;
// Matching with # is over,
// move ahead in pattern
i++;
}
}
return (j == lenText);
}
// Driver code
int main()
{
string str = "ABABABA";
string pat = "A#B#A";
if (regexMatch(str, pat))
cout << "yes";
else
cout << "no";
return 0;
}
Java
// Java program for pattern matching
// where a single special character
// can match one more characters
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
// Returns true if pat
// matches with text.
public static boolean regexMatch
(String text, String pat)
{
int lenText = text.length();
int lenPat = pat.length();
char[] Text = text.toCharArray();
char[] Pat = pat.toCharArray();
// i is used as an index in pattern
// and j as an index in text.
int i = 0, j = 0;
// Traverse through pattern
while (i < lenPat)
{
// If current character of
// pattern is not '#'
if (Pat[i] != '#')
{
// If does not match with text.
if (Pat[i] != Text[j])
return false;
// If matches, increment i and j
i++;
j++;
}
// Current character is '#'
else
{
// At least one character
// must match with #
j++;
// Match characters with # until
// a matching character is found.
while (Text[j] != Pat[i + 1])
j++;
// Matching with # is over,
// move ahead in pattern
i++;
}
}
return (j == lenText);
}
// Driver code
public static void main (String[] args)
{
String str = "ABABABA";
String pat = "A#B#A";
if (regexMatch(str, pat))
System.out.println("yes");
else
System.out.println("no");
}
}
// This code is contributed by Mr. Somesh Awasthi
Python3
# Python3 program for pattern matching
# where a single special character
# can match one more characters
# Returns true if pat matches with
# text
def regexMatch(text, pat):
lenText = len(text)
letPat = len(pat)
# i is used as an index in
# pattern and j as an index
# in text
i = 0
j = 0
# Traverse through pattern
while (i < letPat):
# If current character of
# pattern is not '#'
if (pat[i] != '#'):
# If does not match with
# text
if (pat[i] != text[j]):
return False
# If matches, increment
# i and j
i += 1
j += 1
# Current character is '#'
else:
# At least one character
# must match with #
j += 1
# Match characters with # until
# a matching character is found.
while (text[j] != pat[i + 1]):
j += 1
# Matching with # is over,
# move ahead in pattern
i += 1
return (j == lenText)
# Driver code
if __name__ == "__main__":
st = "ABABABA"
pat = "A#B#A"
if (regexMatch(st, pat)):
print("yes")
else:
print("no")
# This code is contributed by Chitranayal
C#
// C# program for pattern matching
// where a single special character
// can match one more characters
using System;
class GFG
{
// Returns true if pat
// matches with text.
public static bool regexMatch
(String text, String pat)
{
int lenText = text.Length;
int lenPat = pat.Length;
char []Text = text.ToCharArray();
char []Pat = pat.ToCharArray();
// i is used as an index in pattern
// and j as an index in text.
int i = 0, j = 0;
// Traverse through pattern
while (i < lenPat)
{
// If current character
// of pattern is not '#'
if (Pat[i] != '#')
{
// If does not match with text.
if (Pat[i] != Text[j])
return false;
// If matches, increment i and j
i++;
j++;
}
// Current character is '#'
else
{
// At least one character
// must match with #
j++;
// Match characters with # until
// a matching character is found.
while (Text[j] != Pat[i + 1])
j++;
// Matching with # is over,
// move ahead in pattern
i++;
}
}
return (j == lenText);
}
// Driver code
public static void Main ()
{
String str = "ABABABA";
String pat = "A#B#A";
if (regexMatch(str, pat))
Console.Write("yes");
else
Console.Write("no");
}
}
// This code is contributed by nitin mittal
PHP
输出:
yes