Strong Password(贪心思想)

发布时间 2023-07-12 15:56:00作者: o-Sakurajimamai-o
Strong Password
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Monocarp finally got the courage to register on ForceCoders. He came up with a handle but is still thinking about the password.

He wants his password to be as strong as possible, so he came up with the following criteria:

  • the length of the password should be exactly m;
  • the password should only consist of digits from 00 to 99;
  • the password should not appear in the password database (given as a string s) as a subsequence (not necessarily contiguous).

Monocarp also came up with two strings of length ml and r, both consisting only of digits from 00 to 99. He wants the i-th digit of his password to be between li and ri, inclusive.

Does there exist a password that fits all criteria?

Input

The first line contains a single integer t (1t1041≤≤104) — the number of testcases.

The first line of each testcase contains a string s (1|s|31051≤||≤3⋅105), consisting only of digits from 00 to 99 — the password database.

The second line contains a single integer m (1m101≤≤10) — the required length of the password.

The third line contains a string l (|l|=m||=), consisting only of digits from 00 to 99 — the lower restriction on each digit.

The fourth line contains a string r (|r|=m||=), consisting only of digits from 00 to 99 — the upper restriction on each digit. liri≤ for all i from 11 to m.

The sum of lengths of s over all testcases doesn't exceed 31053⋅105.

Output

For each testcase, print "YES" if there exists a password that fits all criteria. Print "NO" otherwise.

Example
input
Copy
5
88005553535123456
2
50
56
123412341234
3
111
444
1234
4
4321
4321
459
2
49
59
00010
2
10
11
output
Copy
YES
NO
YES
NO
YES
Note

In the first testcase, Monocarp can choose password "50". It doesn't appear in s as a subsequence.

In the second testcase, all combinations of three digits, each of them being from 11 to 44, fit the criteria on l and r. However, all of them appear in s as subsequences. For example, "314" appears at positions [3,5,12][3,5,12] and "222" appears at positions [2,6,10][2,6,10].

In the third testcase, Monocarp can choose password "4321". Actually, that is the only password that fits the criteria on l and r. Luckily, it doesn't appear in s as a subsequence.

In the fourth testcase, only "49" and "59" fit the criteria on l and r. Both of them appear in s as subsequences.

In the fifth testcase, Monocarp can choose password "11".

//Strong Password
//贪心的想,每次寻找字母都找在字符串中位置最靠后的,这样出现密码的范围才会变小
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,mod=1e9+7;
int n,t,a[N],f[N],res,num,ans,m;
bool vis[N];
signed main()
{
    std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        res=0;
        string s,l,r;
        cin>>s>>n>>l>>r;
        for(int i=0;i<n;i++){
            int pos=0;
            for(int j=l[i];j<=r[i];j++){
                int x=s.find(j,res);//找到这个字母
                if(x==-1){//如果没有,那就直接判断为可以
                    cout<<"YES"<<endl;
                    goto nexts;
                }
                pos=max(pos,x+1);//如果这个数字位置靠后就更新
            }
            res=max(pos,res);//找到目前位数最靠后的数字
        }
        cout<<"NO"<<endl;
        nexts:;
    }
    return 0;
}