zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    TreeView拖动
    反射机制
    SQLServer2005/2008 XML数据类型操作
    开发与研发:一字之差的感想
    设置在64位机器上的IIS(IIS6/IIS7)兼容32位程序(64位ODBC和32位ODBC的问题同样适用)
    setTimeout和setInterval的使用
    Oracle 安装/使用、配置/卸载
    链接sql数据库以及Oracle 数据库和启动缓存以及停止缓存
    jQuery学习笔记—— .html(),.text()和.val()的使用
    C# List<T>用法
  • 原文地址:https://www.cnblogs.com/dealer/p/12778792.html
Copyright © 2011-2022 走看看