1. 程式人生 > >zcmu 2193 Newspaper Headline(思維+字串處理)

zcmu 2193 Newspaper Headline(思維+字串處理)

ZCMU 2193: Newspaper Headline

Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 26  Solved: 14 [Submit][Status][Web Board]

Description

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

【題意】給你兩個串s1,s2,問構成s2至少需要幾個s1?(可以不連續)

【分析】

  1. 陣列f[i][j]:記錄s1串中第i-1個字母后字母j第一次出現的位置;
  2. 陣列vis[i]:用於判斷判斷s2串中的字母是否都在s1中出現過,順帶記錄所有s1中字母第一次出現的位
  3. 遍歷vis陣列,如果出現-1,說明存在s2中的字元沒有在s1中出現過。則輸出-1;
  4. now記錄當前位置,go記錄下一個位置
  5. 注意,f陣列,開30,而不是直接用字母作為下標,這樣的話複雜度會低很多。
#include<bits/stdc++.h>
using namespace std;
int f[10005][30];//第i個字元後面j字元第一次出現的位置 
int vis[30];
char s1[10005],s2[1000005];
int main()
{
	while(~scanf("%s%s",s1,s2))
	{
		int ans=1;
		int len1=strlen(s1),len2=strlen(s2);
		memset(f,-1,sizeof(f));
		memset(vis,-1,sizeof(vis));
		for(int i=0;i<len1;i++)
		{
			if(vis[s1[i]-97]==-1)vis[s1[i]-97]=i;
			for(int j=i+1;j<len1;j++)
				if(f[i][s1[j]-97]==-1)f[i][s1[j]-97]=j;
		}
		int flag=1;
		for(int i=0;i<len2;i++)
			if(vis[s2[i]-97]==-1)
			{
				puts("-1");
				flag=0;
				break;
			}
		if(!flag)continue;
		int now=vis[s2[0]-97],go;
		for(int i=1;i<len2;i++)
		{
			go=f[now][s2[i]-97];
			if(go==-1)
			{
				ans++;
				now=vis[s2[i]-97];
			}
			else{
				now=go;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
 }