Codeforces 1303 E. Erase Subsequences(DP)

Description:

You are given a string s s s. You can build new string p p p from s s s using the following operation no more than two times:

choose any subsequence s i 1 , s i 2 , … , s i k s_{i_{1}},s_{i_{2}},…,s_{i_{k}} si1,si2,,sik where 1 ≤ i 1 < i 2 < ⋯ < i k ≤ ∣ s ∣ 1≤i_{1}<i_{2}<⋯<i_{k}≤|s| 1i1<i2<<iks;
erase the chosen subsequence from s (s can become empty);
concatenate chosen subsequence to the right of the string p (in other words, p = p + s i 1 s i 2 … s i k p=p+s_{i_{1}}s_{i_{2}}…s_{i_{k}} p=p+si1si2sik).
Of course, initially the string p p p is empty.

For example, let s = a b a b c d s=ababcd s=ababcd. At first, let’s choose subsequence s 1 s 4 s 5 = a b c s_{1}s_{4}s_{5}=abc s1s4s5=abc — we will get s = b a d s=bad s=bad and p = a b c p=abc p=abc. At second, let’s choose s 1 s 2 = b a s_{1}s_{2}=ba s1s2=ba — we will get s = d s=d s=d and p = a b c b a p=abcba p=abcba. So we can build a b c b a abcba abcba from a b a b c d ababcd ababcd.

Can you build a given string t using the algorithm above?

Input

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

Next 2 T 2T 2T lines contain test cases — two per test case. The first line contains string s s s consisting of lowercase Latin letters ( 1 ≤ ∣ s ∣ ≤ 400 ) (1≤|s|≤400) (1s400) — the initial string.

The second line contains string t consisting of lowercase Latin letters ( 1 ≤ ∣ t ∣ ≤ ∣ s ∣ ) (1≤|t|≤|s|) (1ts) — the string you’d like to build.

It’s guaranteed that the total length of strings s s s doesn’t exceed 400 400 400.

Output

Print T T T answers — one per test case. Print Y E S YES YES (case insensitive) if it’s possible to build t and N O NO NO (case insensitive) otherwise.

Example

input

4
ababcd
abcba
a
b
defi
fed
xyz
x

output

YES
NO
NO
YES

题意:

给出两个字符串 s s s p p p,对 s s s 可以操作最多两次,从 s s s 中分离出一个子串,然后加到分离出来的串后面,问最多两次操作能不能把 s s s 变成 p p p

枚举把 t t t 分成哪两段,然后 D P DP DP
t t t 分成两个串, f i j f_{ij} fij 表示在 s s s 中匹配第一个串到 i i i, 匹配第二个串 j j j 的最短长度。
其实匹配这一步可以使用序列自动机。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
    if (a > 9)
        Out(a / 10);
    putchar(a % 10 + '0');
}

ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
///快速幂m^k%mod
ll qpow(ll a, ll b, ll mod)
{
    if (a >= mod)
        a = a % mod + mod;
    ll ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = ans * a;
            if (ans >= mod)
                ans = ans % mod + mod;
        }
        a *= a;
        if (a >= mod)
            a = a % mod + mod;
        b >>= 1;
    }
    return ans;
}

// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
    return qpow(a, p - 2, p);
}

///扩展欧几里得
int exgcd(int a, int b, int &x, int &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    int g = exgcd(b, a % b, x, y);
    int t = x;
    x = y;
    y = t - a / b * y;
    return g;
}

///使用ecgcd求a的逆元x
int mod_reverse(int a, int p)
{
    int d, x, y;
    d = exgcd(a, p, x, y);
    if (d == 1)
        return (x % p + p) % p;
    else
        return -1;
}

///中国剩余定理模板0
ll china(int a[], int b[], int n) //a[]为除数,b[]为余数
{
    int M = 1, y, x = 0;
    for (int i = 0; i < n; ++i) //算出它们累乘的结果
        M *= a[i];
    for (int i = 0; i < n; ++i)
    {
        int w = M / a[i];
        int tx = 0;
        int t = exgcd(w, a[i], tx, y); //计算逆元
        x = (x + w * (b[i] / t) * x) % M;
    }
    return (x + M) % M;
}

const int N = 1000;
char s1[N], s2[N];
int a[N][26];
int f[N][N];
int len1, len2;
bool flag;

void cal(char *s, char *t, int n, int m)
{
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= m; j++)
        {
            if (!i && !j)
                continue;
            f[i][j] = inf;
            if (i && f[i - 1][j] < inf)
                f[i][j] = min(f[i][j], a[f[i - 1][j]][s[i] - 'a']);
            if (j && f[i][j - 1] < inf)
                f[i][j] = min(f[i][j], a[f[i][j - 1]][t[j] - 'a']);
        }
    return;
}

int main()
{
    int t;
    sd(t);
    while (t--)
    {
        ss(s1 + 1);
        ss(s2 + 1);
        len1 = strlen(s1 + 1);
        len2 = strlen(s2 + 1);
        rep(i, 0, len1)
        {
            rep(j, 0, 25)
            {
                a[i][j] = inf;
                rep(k, i + 1, len1)
                {

                    if (s1[k] == 'a' + j)
                    {
                        a[i][j] = k;
                        break;
                    }
                }
            }
        }
        flag = 0;
        rep(i, 1, len2)
        {
            cal(s2, s2 + i, i, len2 - i);
            if (f[i][len2 - i] < inf)
            {
                flag = 1;
                break;
            }
        }
        if (flag)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值