HDU 5414 CRB and String (idea title)

Source: Internet
Author: User

Test instructions

Give you two strings of S and T, you can arbitrarily select a character C in the string s, insert a character d after the character C (d!=c), and ask how many times this operation can convert the string s into a string T.

Analytical:

Don't think too complicated, just discuss it clearly.
1. If |s|>|T| , then no matter how the combination can not constitute T.
2. If s [ 0 ! = t [ 0 , no matter how the combination, also can not constitute T.
3. The above two situations are better considered, then find the longest common prefix of two strings.
If the longest public prefix of S equals |t| indicates that two strings are exactly the same, this can be done by constructing t because the above has been removed |s|>|T| The situation.
4. Since the characters inserted in the following cannot be the same as the characters in front of them, then judging the common prefix of two strings, is not all a letter, if all is a letter, then if the current character to be inserted is the same as the preceding character, then cannot construct T.
5. Exclude 4 of this situation, then you can arbitrarily construct the desired string, because if different, you can arbitrarily construct the following character, the same, you can insert a character after the preceding different characters, equal to construct an identical character.
So the same thing two pointers go forward together, otherwise t's pointer pushes forward to determine if the last S has any remaining.

my code

#include <cstdio>#include <cstring>#include <algorithm>#include <string>using namespace STD;Const intN = (int)1e5+Ten;CharPat[n], tar[n];BOOLJudge () {intLENP =strlen(PAT), LenT =strlen(TAR);if(pat[0]! = tar[0] || LENP > LenT)return false;inti =0, j =0;BOOLdiff =false; while(I < LENP) {if(Pat[i] = = Tar[j]) i++, j + +;Else  Break;if(I >1&& pat[i-1]! = pat[i-2]) diff =true; }if(i = = LenT)return true;if(!diff && tar[j] = = tar[j-1])return false; while(J < LenT) {if(Pat[i] = = Tar[j]) i++;    j + +; }if(i = = LENP)return true;return false;}intMain () {intTscanf("%d", &t); while(t--) {scanf("%s%s", Pat, TAR);puts(Judge ()?"Yes":"No"); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5414 CRB and String (idea title)

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.