Codeforces 559B Equivalent Strings Equivalent string, codeforces559b

Source: Internet
Author: User

Codeforces 559B Equivalent Strings Equivalent string, codeforces559b

Question: give two equal-length strings a and B to determine whether they are equivalent. Equivalent meaning: If the length is odd, it must be the same string. If the length is an even number, the two strings are divided into two substrings (al, ar, bl, and br) Half of the original string. al and bl are equivalent and ar and br are equivalent, or al and br are equivalent, and ar and bl are equivalent.








Actually, it is very watery. Write a Recursive Sub-rule based on the meaning of the question. During the competition, I always thought it would be a battle for brute force writing, because it was probably 4 ^ (log2 (n) Complexity, that is, n ^ 2, so I thought about it during the competition and converted both strings into the smallest string in the Lexicographic Order (the minimum expression of the loop section) according to the meaning of the question) then compare whether the two minimum representations of a and B are the same.

Later, I thought about why the complexity of log2 (n) cannot be achieved after half a day? The reason is: we construct strings according to this complexity. First, if we want to compare al and ar, bl and br, and al and br, and ar and bl are also compared, the al and bl values must be equal, ar and br are not equivalent, and al and br are equivalent, so that ar and bl can be compared. However, when we compare the values of al and bl, we divide the values of al into all, alr, and bl into bll and blr. If we want to make it four more times, in this case, all and bll are equivalent. alr and blr are not equivalent. alr and bll are equivalent. But in this case, al and bl are equivalent, so alr and bll must be equivalent. We simply write

All = bll

Alr! = Blr

Alr = bll

All = blr

However, these four equations can produce all = bll = alr = blr, that is, the four substrings can be equivalent to any other two equations. This indicates that a string cannot be constructed to make the complexity reach 4 ^ (log2 (n )). In fact, in many cases, recursion is performed only three or even two times and the result is returned. Therefore, the efficiency of sub-governance is also very high. Of course, the complexity of the minimum representation is O (n * log (n), which must have passed. In fact, it is still the idea of sub-governance, but the processing is a little different.







# Include <cstdio> # include <iostream> # include <cstring> # include <string> # include <cmath> # include <algorithm> # include <stack> # include <vector> # include <map> # include <set> using namespace std; const int MAX = 2*1e5 + 5; char a [MAX], B [MAX]; int cmp (char * x, int l1, int l2, int len) {for (int I = 0; I <len; I ++) {if (x [l1 + I] <x [l2 + I]) return-1; if (x [l1 + I]> x [l2 + I]) return 1;} return 0;} void tra Nslation (char * x, int l, int r) // convert the original string to the string with the smallest Lexicographic Order {if (r-l + 1) & 1) return; int mid = (l + r)> 1, len = (r-l + 1)> 1; translation (x, l, mid); translation (x, mid + 1, r); if (cmp (x, l, mid + 1, len) <0) {for (int I = 0; I <len; I ++) swap (x [l + I], x [mid + 1 + I]) ;}} void solve () {int lena = strlen (), lenb = strlen (B); translation (a, 0, lena-1); translation (B, 0, lenb-1); printf ("% s \ n", st Rcmp (a, B) = 0? "YES": "NO");} int main () {while (scanf ("% s", a, B )! = EOF) solve (); return 0 ;}

Zookeeper

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.