给定两个字符串text
和pattern
,找到text
中匹配pattern
的最短子字符串的开始和结束索引,这意味着pattern
中的所有字符1}}在子字符串和pattern
中以相同的顺序出现,但这些字符之间可能还有其他字符。
如果你能从text
找到这样的子串,则打印它的开始和结束索引,否则打印-1,-1。如果有多个最短匹配子字符串,则返回具有最小开始索引的子字符串索引。
示例输入:
axxxbcaxbcaxxbc abc
abcd x
axxxbaxbab ab
示例输出:
6 9
-1 -1
8 9
有没有人有一些好的算法来解决这个问题而不使用 内置支持C ++或Python中的正则表达式
答案 0 :(得分:1)
循环文本的字符,找到文本中图案的第一个字符。如果找到它,在剩余的文本中搜索模式的第二个字符的出现,并在模式中的所有字符上重复操作,跳过文本中不需要的字符。完成后,再次启动下一个模式的第一个char文本。
使用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
编辑:您应该尝试从文本末尾开始实现此算法,因为它是您正在寻找的子字符串的位置。
答案 1 :(得分:1)
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;
}