N
N
Nikita Oleinik2015-12-27 23:28:32
IT education
Nikita Oleinik, 2015-12-27 23:28:32

How to check if a word matches a pattern?

As a preparation for the Olympiad in Informatics (Grade 10), the following task was given: a word and a template were given. You need to find out if the word fits this pattern. The word is a sequence of Latin letters, and the template can also contain the symbols ? and *. Question mark - only one character can stand in this place, in place of asterisks - a sequence of characters, possibly empty. Example:
Word: iwjvjmcquurlgix
Pattern: i**j*j*?u?u*
The answer is no, as there must be at least one character between two U's.
Help me decide, push me to the right thoughts. What came to my mind - finding the greatest common subsequence through DP (and then checking for equality with a template without the symbols ? and *), of course, does not always work. Does this problem have any slightly beautiful solution, not through tons of code to "implement" (my second brilliant idea)? Googled and searched a lot on Habré. I write in C++.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Adamos, 2015-12-28
@nikitosoleil

You need to match the pattern, character by character, with the word.
The letter matches (? always matches) - go to the next one.
Does not match - go to the fallback, if they run out - the answer is "no".
Fallbacks are created on asterisks - by enumerating how many characters can be hidden under it.
Create an abstraction (class, structure) - the state of the template check on a certain letter, this will be both the current state and each of the saved options.
Yes, in science this is called a finite state machine.

V
Vladimir Martyanov, 2015-12-27
@vilgeforce

state machine, which is also a finite state machine, may well come up.

X
xmoonlight, 2015-12-28
@xmoonlight

regex

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question