![](/img/trans.png)
[英]'(match_start1)…(match_start2)…(match_end)' find the shortest string match
[英]find the begin and end index of shortest substring in a string that match another pattern
給定兩個字符串text
和pattern
,在匹配pattern
text
中找到最短子字符串的開始和結尾索引,這意味着pattern
中的所有字符在子字符串和pattern
中都以相同的順序出現,但是在這兩個字符串之間可能還有其他字符字符。
如果可以從text
找到這樣的子字符串,則打印其開始和結束索引,否則打印-1,-1。 如果存在多個最短匹配的子字符串,則返回具有最小開始索引的子字符串的索引。
輸入樣例:
axxxbcaxbcaxxbc abc
abcd x
axxxbaxbab ab
樣本輸出:
6 9
-1 -1
8 9
沒有人有一些好的算法可以解決此問題,而無需使用C ++或Python中對正則表達式的內置支持
循環遍歷文本的字符,並在文本中找到模式的第一個字符。 如果找到它,請在其余文本中搜索模式的第二個字符,然后對模式中的所有字符重復該操作,跳過文本中不需要的字符。 完成后,從下一次出現模式的第一個字符在文本中重新開始。
也許使用abc
模式更直觀:
axxxbcaxbcaxxbc
[axxx|b|c] -> 6 chars
[ax|b|c] -> 4 chars
[axx|b|c] -> 5 chars
要么
aababaccccccc
[aa|baba|c] -> 6 chars
[a|baba|c] -> 5 chars
[a|ba|c] -> 4 chars
[accccccc] -> -1 chars as the substring does not match the pattern
編輯:您應該嘗試從文本結尾開始實施該算法,因為它是您正在尋找的子字符串最有可能出現的地方。
def shortest_match(text, pattern):
stack = [] # to store matches
for i in range(len(text) - len(pattern) + 1):
# if we match the firts character of pattern in
# text then we start to search for the rest of it
if pattern[0] == text[i]:
j = 1 # pattern[0] already match, let's check from 1 onwards
k = i + 1 # text[i] == pattern[0], let's check from text[i+1] onwards
# while pattern[j] could match text[i]
while j < len(pattern) and k < len(text):
if pattern[j] == text[k]:
j += 1 # pattern[j] matched. Let's move to the next character
k += 1
if j == len(pattern): # if the match was found add it to the stack
stack.append((i, k-1))
else: # otherwise break the loop (we won't find any other match)
break
if not stack: # no match found
return (-1, -1)
lengths = [y - x for x, y in stack] # list of matches lengths
return stack[lengths.index(min(lengths))] # return the shortest
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
struct match_pair
{
int start;
int end;
int length;
};
void
print_match (match_pair m)
{
cout << "(" << m.start << ", " << m.end << ")";
}
match_pair
shortest_match (char * text, char * pattern)
{
vector <match_pair> stack; // to store matches
for (int i = 0; strlen(text) - strlen(pattern) + 1; ++i)
{
// if we match the firts character of pattern in
// text then we start to search for the rest of it
if (pattern[0] == text[i])
{
int j = 1; // pattern[0] already match, let's check from 1 onwards
int k = i + 1; // text[i] == pattern[0], let's check from text[i+1] onwards
// while pattern[j] could match text[i]
while (j < strlen(pattern) && k < strlen(text))
{
if (pattern[j] == text[k])
{
++j; // pattern[j] matched. Let's move to the next character
}
++k;
}
if (j == strlen(pattern)) // if the match was found add it to the stack
{
match_pair current_match;
current_match.start = i;
current_match.end = k - 1;
current_match.length = current_match.end - current_match.start;
stack.push_back(current_match);
} else // otherwise break the loop (we won't find any other match)
{
break;
}
}
}
match_pair shortest;
if (stack.empty()) // no match, return (-1, -1)
{
shortest.start = -1;
shortest.end = -1;
shortest.length = 0;
return shortest;
}
// search for shortest match
shortest.start = stack[0].start;
shortest.end = stack[0].end;
shortest.length = stack[0].length;
for (int i = 1; i < stack.size(); ++i)
{
if (stack[i].length < shortest.length)
{
shortest.start = stack[i].start;
shortest.end = stack[i].end;
shortest.length = stack[i].length;
}
}
return shortest;
}
// override << for printing match_pair
std::ostream&
operator<< (std::ostream& os, const match_pair& m)
{
return os << "(" << m.start << ", " << m.end << ")";
}
int
main ()
{
char text[] = "axxxbcaxbcaxxbc";
char pattern[] = "abc";
cout << shortest_match(text, pattern);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.