HDU 5414 CRB and String (greedy)

Source: Internet
Author: User

Title: http://acm.hdu.edu.cn/showproblem.php?pid=5414

Test instructions: Given the string s and T, you can select a character C in S, and then choose a character D (d! =C) Insert D into the back of C and ask if you can change s to T.

Analysis: First consider the character (string) can be inserted in the case, if the string "SD" character s after the string P (|p|>=1), as long as p[0]!= ' s ' line, such as p= "Oola", the first O is inserted in the ' s ' behind, the second also inserted in the ' s ' behind, is OK. Then the characters in S and T are matched and the subscript of the character in T is as far back as possible (from backward to forward), indicating which characters in T are fixed within S. The first character of the string in the middle of the two-mark character is then directly determined to satisfy the condition. Special cases "AAA" and "AASAA", if the next two a matches the words obviously not, in the past, when judging by the way to modify the matching value on the line.

Code:

#include <iostream> #include <cstring> #include <cstdio>using namespace std;const int maxn = 1e5+6;char S1[maxn],s2[maxn];int Match[maxn];int Main () {int ncase,len1,len2,i,j,no;scanf ("%d", &ncase), while (ncase--) { scanf ("%s%s", s1,s2); Len1=strlen (S1); Len2=strlen (S2); if (S1[0]!=s2[0] | | len1>len2) {printf ("no\n"); continue;} NO = 0;j=len2-1;for (i=len1-1;i>=1;i--) {while (j>=0 && s2[j]!=s1[i]) j--;if (j<1) {no=1;break;} match[i]=j--;} Match[0]=0;if (NO) {printf ("no\n"); continue;} for (i=0;i<len1;i++) {if (i+1<len1 && match[i+1]==match[i]+1) continue; if (S2[match[i]+1]==s1[i]) {if (i+ 1<len1 && s1[i+1]==s2[match[i]+1]) {match[i+1]=match[i]+1;continue;} No=1;break;}} if (NO) printf ("no\n"); elseprintf ("yes\n");} return 0;}


HDU 5414 CRB and String (greedy)

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.