• C. Obtain The 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 (1T1001≤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 21052⋅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;
    }
  • 相关阅读:
    LeetCode 252. Meeting Rooms
    LeetCode 161. One Edit Distance
    LeetCode 156. Binary Tree Upside Down
    LeetCode 173. Binary Search Tree Iterator
    LeetCode 285. Inorder Successor in BST
    LeetCode 305. Number of Islands II
    LeetCode 272. Closest Binary Search Tree Value II
    LeetCode 270. Closest Binary Search Tree Value
    LeetCode 329. Longest Increasing Path in a Matrix
    LintCode Subtree
  • 原文地址:https://www.cnblogs.com/dealer/p/12778792.html
Copyright © 2020-2023  润新知