Description

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.

Sample Input

Input
abc
xyz
Output
-1
Input
abcd
dabc
Output
2

【题意】给出两个字符串a,b,a可以接无数个a在每个a后面,问至少接几个a才能出现b,(可以是不连续的)

【思路】清题,看了大神的代码,顿感厉害,还学了一个upper_bound函数。

参考:https://www.ocrosoft.com/?p=1362

1.记录a有的字母,如果b有的字母a没有,那么就是-1。
2.记录a中每个字母的索引(位置),使用26个set比较方便。
然后对b的每一个字母进行匹配,同时还要记录一下上一个字母匹配到的位置last(初始化-1)。
如果下一个字母匹配的时候如果last比这个字母所有的索引都大,说明要再拼接一个a字符串,也就是答案加一。
否则,选择大于last的第一个位置进行匹配。
这个操作可以使用upper_bound函数,返回大于last的位置,也可以不用,手动二分。

#include<iostream>
#include<stdio.h>
#include<string>
#include<set>
#include<string.h>
using namespace std;
const int N=1e6+;
int main()
{
string a,b;
set<int>s[];
cin>>a>>b;
int len1=a.size(),len2=b.size();
for(int i=;i<len1;i++)
s[a[i]-'a'].insert(i);
int k=-,ans=;
for(int i=;i<len2;i++)
{
int tmp=b[i]-'a';
if(s[tmp].empty())
{
printf("-1\n");
goto final;
}
set<int>::iterator it=s[tmp].upper_bound(k);
if(it==s[tmp].end())
{
k=-;
ans++;
}
k=*s[tmp].upper_bound(k);
}
printf("%d\n",ans);
final:
return ;
}

Newspaper Headline_set(upper_bound)的更多相关文章

  1. STL源码学习----lower_bound和upper_bound算法

    转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...

  2. 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound

    [什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...

  3. STL_lower_bound&upper_bound用法

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

  4. STL之lower_bound和upper_bound

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

  5. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  6. [STL] lower_bound和upper_bound

    STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...

  7. STL lower_bound upper_bound binary-search

    STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...

  8. vector的插入、lower_bound、upper_bound、equal_range实例

    对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...

  9. STL中的lower_bound和upper_bound的理解

    STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...

随机推荐

  1. Java 基础知识点(必知必会其一)

    如何将字符串转换为数字? package Day_2; /** * @author Administrator * 功能: 如何将字符串转换为数字? */ public class StringToI ...

  2. hdu----(5055)Bob and math problem(贪心)

    Bob and math problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. Idea添加Jetty时提示JMX module is not included

    添加自己的jetty时提示下图 此时,我们应该编辑jetty根目录下 start.ini 添加 "--module=jmx" 此时就可以成功添加了 原因:因为在9.07的时候默认情 ...

  4. [转]Visual Studio 2010 单元测试目录

    Visual Studio 2010 单元测试共分七个部分: 普通单元测试.顺序单元测试.压力测试,Generic测试.数据库测试.UI界面测试和Web性能测试. 这个系列的博客实例程序可以在下面的链 ...

  5. Loadrunner 使用检查点

    在使用loadrunner 测试时,比如我们在登陆时,我们希望说登录是否成功.这个时候我可以使用检查点函数. 检查登录后,页面上是否返回了某个标识,这样我们可以根据标识判断登录事务是否成功.   现在 ...

  6. Octopus系列之开发过程各个技术点

    自定义了页面周期 使用唯一的一个VelocityEngine全局的静态实例,优化了小泥鳅blog中每次请求都要创建VelocityEngine实例对象,减少了对象的开销 通过UA判断请求来自的设备,从 ...

  7. 深入理解Redis:底层数据结构

    简介 redis[1]是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...

  8. POJ 2187 求凸包上最长距离

    简单的旋转卡壳题目 以每一条边作为基础,找到那个最远的对踵点,计算所有对踵点的点对距离 这里求的是距离的平方,所有过程都是int即可 #include <iostream> #includ ...

  9. POJ3624

    题目大意: 给出珠宝的重量Wi和珠宝的价值Di,并给定一个重量范围M,在不超过M的情况下求取到的珠宝的最大值,N为列出珠宝的重量. #include <iostream> #include ...

  10. 【干货来了】2014年K2房地产IT分享峰会

    2014年K2房地产IT分享峰会已圆满落幕,嘉宾们纷纷出招,分享干货,现场妙语连珠不断,高潮迭起. 主题:流程驱动的地产业务管控平台 嘉宾:王寿欣(卓越地产战略与运营管理部 副总经理) 卓越地产应用K ...