C. Obtain The String--------------------思维

You are given two strings s and t consisting of lowercase Latin letters. Also you have a string z which is initially empty. You want string z to be equal to string t. You can perform the following operation to achieve this: append any subsequence of s at the end of string z. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z=ac, s=abcde, you may turn z into following strings in one operation:

z=acace (if we choose subsequence ace);
z=acbcd (if we choose subsequence bcd);
z=acbce (if we choose subsequence bce).
Note that after this operation string s doesn’t change.

Calculate the minimum number of such operations to turn string z into string t.

Input
The first line contains the integer T (1≤T≤100) — the number of test cases.

The first line of each testcase contains one string s (1≤|s|≤105) consisting of lowercase Latin letters.

The second line of each testcase contains one string t (1≤|t|≤105) consisting of lowercase Latin letters.

It is guaranteed that the total length of all strings s and t in the input does not exceed 2⋅105.

Output
For each testcase, print one integer — the minimum number of operations to turn string z into string t. If it’s impossible print −1.

Example
inputCopy

3
aabce
ace
abacaba
aax
ty
yyt
outputCopy
1
-1
3

题意:
给你两串一个s串和一个t串,问你能不能从s串中找到子序列使得等于t串

解析:
我们把s串的每个字符出现的位置都存起来。
然后遍历t串,去二分遍历t串中每个字符的位置。如果二分完了,说明我们需要再来一步,从头开始继续二分。直到遍历完。

第二种做法:序列自动机
代码有注释


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
string s,t;
int k;
set<int> v[N];
int main()
{
	scanf("%d",&k);
	while(k--)
	{
		for(int i=0;i<N;i++) v[i].clear();
		cin>>s>>t;
		int n=s.size();
		int m=t.size();
		for(int i=0;i<n;i++)
		{
			v[s[i]-'a'].insert(i); // 把每个字符的位置存储起来
		}
		int ans=1;
		int b=0;
		for(int i=0;i<m;i++)
		{
			if(v[t[i]-'a'].size()==0)  //如果t串中的字符再s串中没有,只能输出-1
			{
				ans=-1;
				break;
			}
			if(v[t[i]-'a'].lower_bound(b)==v[t[i]-'a'].end()) //去二分查找位置,如果位置在最后一个,说明我们这次的操作就结束了,需要从头开始了。
			{
				ans++;
				b=0;
			}
			b=*v[t[i]-'a'].lower_bound(b); //没有二分到最后,那我们就记录当前的位置,下次二分时,我们需要从b+1开始找了。
			b++;
		}
		cout<<ans<<endl;
	}
}

2.14在更新一下序列自动机的做法

#include<bits/stdc++.h>
using namespace  std;
#define INF 0x3f3f3f3f
const int N=1e5+1000;
int cnt[N][26];
int n;
string s,t;
void init(string s)
{
	int n=s.size(); //第i个位置后,字符j出现的第一个位置 
	for(int i=0;i<26;i++)  cnt[n][i]=INF;
	for(int i=n-1;i>=0;i--)
	{
		for(int j=0;j<26;j++) cnt[i][j]=cnt[i+1][j];
		cnt[i][s[i]-'a']=i;
			
	}
}
int main()
{
	cin>>n;
	while(n--)
	{
		cin>>s>>t;
		init(s);
		//cout<<cnt[1]['y'-'a']<<endl;
		int pos=-1;
		int ans=1;
		int m=t.size();
		for(int i=0;i<m;i++)
		{
			if(cnt[0][t[i]-'a']==INF)//从第i个位置开始   ,t[i]-'a'字符如果没出现过,肯定不满足条件
			{
				ans=-1;
				break; 
			} 
			if(cnt[pos+1][t[i]-'a']!=INF) pos=cnt[pos+1][t[i]-'a'];
			else
			{
				ans++;
				pos=cnt[0][t[i]-'a'];
			}
		}
		cout<<ans<<endl;
	} 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值