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

【题意】给你两个串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;
 } 

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/82941391
今日推荐