ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C. Obtain The String

2020-04-26 12:52:25  阅读:310  来源: 互联网

标签:const string int tt Obtain zz include String


You are given two strings ss and tt consisting of lowercase Latin letters. Also you have a string zz which is initially empty. You want string zz to be equal to string tt. You can perform the following operation to achieve this: append any subsequence of ss at the end of string zz. 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=acz=ac, s=abcdes=abcde, you may turn zz into following strings in one operation:

  1. z=acacez=acace (if we choose subsequence aceace);
  2. z=acbcdz=acbcd (if we choose subsequence bcdbcd);
  3. z=acbcez=acbce (if we choose subsequence bcebce).

Note that after this operation string ss doesn't change.

Calculate the minimum number of such operations to turn string zz into string tt.

Input

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

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

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

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

Output

For each testcase, print one integer — the minimum number of operations to turn string zz into string tt. If it's impossible print −1−1.

Example input Copy
3
aabce
ace
abacaba
aax
ty
yyt
output Copy
1
-1
3
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 100007;
const int N = 1005;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        string s, t;
        cin >> s >> t;
        vector<vector<int>> a(26);
        for (int i = 0; i < s.size(); i++)
        {
            a[(s[i] - 'a')].push_back(i);
        }
        int cant=0,res=1,last=-1;
        for (int i = 0; i < t.size(); i++)
        {
            if (a[t[i]-'a'].size() == 0)
            {
                cant = 1;
                break;
            }
            if (upper_bound(a[t[i] - 'a'].begin(), a[t[i] - 'a'].end(), last) == a[t[i] - 'a'].end())
            {
                res++;
                last = -1;
                i--;
            }
            else
            {
                last = *upper_bound(a[t[i] - 'a'].begin(), a[t[i] - 'a'].end(), last);
            }
        }
        if (cant)
            cout << -1 << endl;
        else
            cout << res << endl;
    }
    return 0;
}

 

标签:const,string,int,tt,Obtain,zz,include,String
来源: https://www.cnblogs.com/dealer/p/12778792.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有