Codeforces 223B Two strings

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.