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 ;
}

最新文章

  1. [MongoDB]对数组操作
  2. 洛谷P1017 进制转换
  3. 图表控件Edraw Max免费下载地址
  4. Tomcat在eclipse中起动成功,主页却打不开
  5. 两款HTTP流量分析工具HttpWatch与Fiddler的比较(转)
  6. BPMN这点事-BPMN扩展元素
  7. C#右键复制路径
  8. 使用atomic一定是线程安全的吗
  9. Solr系列一:Solr与Tomcat的整合
  10. Entity Framework 的枚举类型
  11. Ajax XMLHttpRequest对象的三个属性以及open和send方法
  12. Ubuntu下安装Mysql并使用
  13. 在2002年的老电脑上安装Debian
  14. [ZOJ3435]Ideal Puzzle Bobble
  15. springMVC 多文件上传前后台demo
  16. CentOS(Linux)中解决MySQL乱码
  17. JavaSE——UDP协议网络编程(二)
  18. BSGS
  19. apache伪静态规则解析
  20. RabbitMQ的应用场景以及基本原理介绍 【转】

热门文章

  1. 经典DP 二维换一维
  2. NimBus一个好的开发框架
  3. 使用ROS节点(五)
  4. android button text属性中英文大小写问题
  5. Rhel6-tomcat+nginx+memcached配置文档
  6. Codeforces Round #327 (Div. 2)-Wizards&#39; Duel
  7. 赋值运算符、拷贝初始化和this指针
  8. vs2013的使用和单元测试
  9. Apache Jmeter(2)
  10. goldengate复制过程字符集处理一例