Algorithm


A. Newspaper Headline
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A newspaper is published in Walrusland. Its heading is s1, it consists of lowercase Latin letters. Fangy the little walrus wants to buy several such newspapers, cut out their headings, glue them one to another in order to get one big string. After that walrus erase several letters from this string in order to get a new word s2. It is considered that when Fangy erases some letter, there's no whitespace formed instead of the letter. That is, the string remains unbroken and it still only consists of lowercase Latin letters.

For example, the heading is "abc". If we take two such headings and glue them one to the other one, we get "abcabc". If we erase the letters on positions 1 and 5, we get a word "bcac".

Which least number of newspaper headings s1 will Fangy need to glue them, erase several letters and get word s2?

Input

The input data contain two lines. The first line contain the heading s1, the second line contains the word s2. The lines only consist of lowercase Latin letters (1 ≤ |s1| ≤ 104, 1 ≤ |s2| ≤ 106).

Output

If it is impossible to get the word s2 in the above-described manner, print "-1" (without the quotes). Otherwise, print the least number of newspaper headings s1, which Fangy will need to receive the word s2.

Examples
input
Copy
abc
xyz
output
Copy
-1
input
Copy
abcd
dabc
output
Copy
2



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
#include <algorithm>
#include <vector>

int main(){

    const int N = 26;

    std::string source, target;
    getline(std::cin, source);
    getline(std::cin, target);

    std::vector<std::vector < long> > pos(N);
    for(size_t p = 0; p < source.size(); p++){pos[source[p] - 'a'].push_back(p);}

    long count(1);
    long index = -1;
    for(size_t p = 0; p < target.size(); p++){
        int letter = target[p] - 'a'; 
        if(pos[letter].empty()){count = -1; break;}
        std::vector<long>::iterator posPointer = std::upper_bound(pos[letter].begin(), pos[letter].end(), index);
        if(posPointer == pos[letter].end()){++count; index = -1;}
        index = *std::upper_bound(pos[letter].begin(), pos[letter].end(), index);
    }

    std::cout << count << std::endl;

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
abc
xyz

Output

x
+
cmd
-1
Advertisements

Demonstration


Codeforces Solution-A. Newspaper Headline-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+