给定两个字符串textpattern,找到与text匹配的pattern中最短子字符串的开始和结束索引,这意味着pattern中的所有字符在子字符串和pattern中都以相同的顺序出现,但这些字符之间可能还有其他字符。
如果您可以从text中找到这样的子字符串,请打印它的开始和结束索引,否则打印-1,-1如果存在多个最短匹配子串,则返回起始索引最小的子串的索引。
样本输入:

axxxbcaxbcaxxbc abc

abcd x

axxxbaxbab ab

样本输出:
6 9

-1 -1

8 9

有没有人在不使用
C++或Python中正则表达式的构建支持

最佳答案

蟒蛇。

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

C++
#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;
}

关于python - 在匹配另一个模式的字符串中找到最短子字符串的开始和结束索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46304391/

10-11 19:37
查看更多