Bajka
Time limit1sMemory limit512 MB
Find the least time to write word t by moving a finger over word s, where adjacent moves write a letter and same-letter jumps write nothing.
- Level
Medium6 of 10
- Topics
- Dynamic programming, String
- Solved
- No attempts yet
Problem
Little Fabijan got bored with picture books, so he decided to read his first fairytale. Unfortunately, Fabijan often encounters a word that scares him. To overcome his fear, he will play a game he invented.
The scary word is an array of lowercase letters. To start the game, Fabijan puts his finger on some position of the array and writes the letter at that position on a piece of paper. He then performs one of the following moves any number of times:
- He moves the finger to a position one place to the left or to the right of the current position, if that position exists. He then writes the letter at the new position on the paper, after the last written letter.
- He moves the finger to any position with the same letter as the current one. He writes nothing on the paper in this case.
Moving the finger from position to position takes seconds.
Fabijan overcomes his fear if his favourite word is written on the paper at the end of the game. He wants to finish the fairytale as soon as possible, so find the minimum number of seconds needed to write his favourite word.
Input
The first line contains two integers and ().
The second line contains lowercase letters, the scary word.
The third line contains lowercase letters, Fabijan's favourite word.
Output
Print the shortest possible time in seconds needed to write the favourite word on the paper, or if that is not possible.
Hint
Clarification of the third sample: Fabijan first puts his finger on position 7 and writes down the letter 'b'. He then moves the finger two times to the left, and each time writes down the letter 'o'. Next, he moves the finger to position 6 using the second type of move. Finally, he moves the finger two times to the left and writes down the letters 'o' and 'k'. Each move takes one second, so the total time is 5 seconds.