[Huawei OD Unified Exam B Paper | 100 points] Enhanced strstr (C++ Java JavaScript Python)

Online OJ

Users who have already purchased this column, please private message the blogger to open an account and brush up questions online! ! !

Online OJ: brush the questions immediately

Question bank column: 2023 Huawei OD machine test (A volume + B volume) (C++JavaJSPy)

Title description: Enhanced strstr

There is a library function in C language: char *strstr(const char *haystack, const char *needle) to find the position where the string needle appears for the first time in the string haystack, and return null if not found.

It is now required to implement an enhanced function of strstr, which can use strings with optional segments to perform fuzzy queries, and return the first searched string position just like strstr.

The optional section is marked with "[]", which means that any character in the optional section can satisfy the matching condition. For example, "a[bc]" means that it can match "ab" or "ac".

Note that optional segments may appear multiple times in the target string.

enter description

Like the strstr function, the input parameters are two string pointers, the source string and the target string.

output description

Different from the strstr function, what is returned is the offset of the matched substring relative to the address of the source string in the source string (starting from 0), and -1 if there is no match.

Supplementary note: '[]' must not be included in the source string; '[]' must appear in pairs in the target string, and there will be no nesting

Guess you like

Origin blog.csdn.net/shangyanaf/article/details/131749186