Codeforces 91A-Newspaper Headline

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
abc
xyz
output
-1
input
abcd
dabc
output
2

题意:给定一个A串,一个B串,用不同的去除某些位置的A串组成B串,问最少需要几串?

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>

using namespace std;

char s1[10009],s2[1000009];
int a[10009][26],b[26];

int main()
{
    while(~scanf("%s %s",s1,s2))
    {
        memset(a,-1,sizeof(a));
        memset(b,0,sizeof b);
        int sum=0,flag=0;
        int len1=strlen(s1),len2=strlen(s2);
        for(int i=0; i<len1; i++)
        {
            b[s1[i]-'a']=1;
            for (int j=0; j<=i; j++)
                if (a[j][s1[i]-'a']==-1) a[j][s1[i]-'a']=i;
        }
        for (int i=0; i<len2; i++)
            if (!b[s2[i]-'a'])
            {
                printf("-1\n");
                flag=1;break;
            }
        if(flag) continue;
        int j=0;
        while(j<len2)
        {
            sum++;
            int i=0;
            while(j<len2&&i<len1&&a[i][s2[j]-'a']!=-1)
            {
                i=a[i][s2[j]-'a']+1;
                j++;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值