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;
	}
}
发布了383 篇原创文章 · 获赞 7 · 访问量 8038

猜你喜欢

转载自blog.csdn.net/qq_43690454/article/details/104248370