CodeForces 91A Newspaper Headline
题目描述
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.
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 s 1 will Fangy need to glue them, erase several letters and get word s 2 ?
输入描述:
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).
输出描述:
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.
输出
复制-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; }