A newspaper is published in Walrusland. Its heading is s 1 , 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 s 2 . 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.ios
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".ide
Which least number of newspaper headings s 1 will Fangy need to glue them, erase several letters and get word s 2 ?this
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).spa
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.code
-1
2
題意:給出兩個字符串s1,s2,問須要幾個s1字符串才能獲得s2的序列(能夠是不連續的)
#include<iostream> #include<algorithm> #include<math.h> #include<string.h> #include<set> #define ll long long #define M 0x3f3f3f3f3f using namespace std; string s1,s2; set<int>s[26]; int main() { while(cin>>s1>>s2) { for(int i=0;i<26;i++) s[i].clear(); int flag=0,cnt=1,last=-1; for(int i=0;s1[i];i++) s[s1[i]-'a'].insert(i); for(int i=0;s2[i];i++) { if(s[s2[i]-'a'].size()==0) { flag=1; break; } else { set<int>::iterator it=s[s2[i]-'a'].upper_bound(last); if(it==s[s2[i]-'a'].end()) { cnt++; last=-1; } last=*s[s2[i]-'a'].upper_bound(last); } } if(flag==1) cout<<-1<<endl; else cout<<cnt<<endl; } return 0; }