A. Newspaper Headline

题目: 

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.

大体题意:

 是否能把n个s1字符串链接起来得到一个长字符串,通过去掉长字符串中任意个字母后得到s2,

如果n存在则求出n的最小值,否则输出“-1”.

思路:

1.首先进行判定是否存在n,即判断b是否有a中没有的字母,如果有则必定不存在n,
 如果没有则必定存在n

2.处理必定存在n的情况,在while循环中不断遍历a字符串,如果碰到字符串b的元素与a中的元素相同则(j+1),如果字符串a遍历完了j还是<strlen(b)则k++进入下一轮循环。直到j==strlen(b)结束循环,此时,k即为所求n

#include <iostream>
#include<bits/stdc++.h>
#include <algorithm>
using namespace std;
char a[10005],b[1000005];
	int zm[200]={0};//用一个数组统计a中出现过的字母 
	int cs=0;
int main(){
	cin>>a;
	cin>>b;
	for(int i=0;i<strlen(a);i++){
		zm[a[i]]=1;//以字母为下标
	}
	int flag=0;
	for(int i=0;i<strlen(b);i++){
		if(zm[b[i]]!=1){
			flag=1;
			/*判断是否有a中没有的字母,如果有则必定不存在n,
			如果没有则必定存在n*/ 
			break;
		}
	}
	if(flag==0){//必定存在n的情况 
		int k=1,j=0;
		if(strlen(b)!=0){//b字符串非空的情况 
			while(j<strlen(b)){
			for(int i=0;i<strlen(a);i++){
				if(b[j]==a[i]) j++;
				if(j==strlen(b)) break;//达到条件退出 
			}
			if(strlen(b)>j) k++;//不符合条件循环继续 
		}
		cout<<k;
		}
		else cout<<"0";	//b字符串为空的情况 
	}
	else cout<<"-1";
	return 0;
} 

Input

abc
xyz

Output

-1

Input

abcd
dabc

Output

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值