A. Newspaper Headline
time limit per test:2 second
memory limit per test:256 megabytes
input:standard input
output:standard output
题目链接:https://codeforces.com/problemset/problem/91/A
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
题意
把最少n个字符串s1拼起来,得到拼接好的字符串,删去若干字符,得到字符串s2
思路
按顺序寻找字符串s2的各个字符,因为s2的各个字符的相对位置在拼接的字符串中是不变的,所以一遍一遍在s1中寻找s2的各个字符即可,记录s1被寻找了几遍,即最少需要多少字符串s1
代码
#include <iostream>
#include <cstring>
using namespace std;
char s1[10010],s2[1000010];
bool st1[26],st2[26];//st1、st2分别记录字符串s1、s2中哪些字符出现了
int main()
{
cin>>s1+1>>s2+1;
int len1=strlen(s1+1);
int len2=strlen(s2+1);
for(int i=1;i<=len1;i++)st1[s1[i]-'a']=1;
for(int j=1;j<=len2;j++)st2[s2[j]-'a']=1;
for(int i=0;i<26;i++)
{
if(st2[i]==1&&st1[i]==0)//如果s2中出现的字母s1中没有出现就输出-1
{
cout<<-1<<endl;
return 0;
}
}
int ans=1,j,i=1;
while(1)
{
for(j=1;j<=len1;j++)
{
if(s1[j]==s2[i])i++;
if(i>len2)goto here;
}
ans++;
}
here:
cout<<ans<<endl;
return 0;
}