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
abc xyz
Output
-1
Input
abcd dabc
Output
2
意思:就是俩个字符串 s1,s2 s1可以是n个s1相加,然后 从s1中去掉一些字符,可以让这个字符串等于s2
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s1,s2;
cin>>s1>>s2;
set<int>s[30];
for(int i=0;i<s1.size();i++) s[s1[i]-'a'].insert(i);
int flag=-1;
int ans=1;
for(int i=0;i<s2.size();i++){
int t=s2[i]-'a';
if(s[t].empty()){//如果在s1中找不到 那么输出-1 然后结束
puts("-1");
return 0;
}
set<int>::iterator it=s[t].upper_bound(flag);//看上一个是否超过了s1的长度
if(it==s[t].end()){//如果超过了,那么就加个s1
flag=-1;//重新定义flag值 ,然后更新为当前t在s1中的下标值
ans++;
}
flag=*(s[t].upper_bound(flag));//更新这个下标,看到哪了,留给下一次循环用来判断
}
printf("%d\n",ans);
return 0;
}