I just want to exercise my expression skills.
Here are two strings, S and T. Dye all the subsequences in S (which must be the same as the T string), and finally ask if you can fully dye S, that is, each character will be stained
Train of Thought: first from left to right match, find the position where the current character in S can be matched to the T string at the maximum, and then match again in turn, find the position in S where the leftmost character can match the T string.
For example
S: ababbaba
T: aba
Then, the rightmost of 'B' at the positions 4th and 5 in S can be matched to the position in T, which is the last 'B' (second character in S) position in the rightmost matching T string
Finally, you can determine whether the leftmost and rightmost matching locations are cross.
[Cpp]
# Include <cstdio>
# Include <cstring>
Const int maxn = 200010;
Int L [maxn], R [maxn];
Char s [maxn], t [maxn];
Int lastpos [maxn];
Int main ()
{
Scanf ("% s", s, t );
Int ls = strlen (s );
Int lt = strlen (t );
Memset (lastpos,-1, sizeof (lastpos ));
Int cur = 0;
L [0] = s [0] = t [0]? 0:-1;
For (int I = 0; I <ls; I ++)
{
L [I] = lastpos [s [I]-'a'];
If (cur <lt & s [I] = t [cur])
{
L [I] = cur;
Cur ++;
}
Lastpos [s [I]-'a'] = L [I];
// Printf ("I = % d \ n", I, L [I]);
}
Memset (lastpos,-1, sizeof (lastpos ));
Cur = lt-1;
R [ls-1] = s [ls-1] = t [cur]? Cur:-1;
For (int I = ls-1; I> = 0; I --)
{
R [I] = lastpos [s [I]-'a'];
If (cur> = 0 & s [I] = t [cur])
{
R [I] = cur;
Cur --;
}
Lastpos [s [I]-'a'] = R [I];
// Printf ("I = % d \ n", I, R [I]);
}
For (int I = 0; I <ls; I ++)
{
If (L [I] =-1 | R [I] =-1 | L [I] <R [I]) return printf ("NO \ n"), 0;
}
Printf ("YES \ n ");
Return 0;
}