Codeforces 559B equivalent Strings equivalent string

Source: Internet
Author: User

Test instructions: Given two equal long strings, a, B, the judgment is equivalent. Equivalence means: If the length is odd, it must be the same string. If the length is even, the two strings are divided into two substrings of the length of the original string Al,ar and Bl,br, where Al and BL are equivalent and AR and BR equivalent, or Al and BR equivalent and AR and BL equivalent.








actually very water. Directly according to test instructions simulation to write a recursive division can be asked. The game always felt this violent writing will be tle, because it is probably 4^ (log2 (n)) of the complexity, that is, n^2, so the game when the thought of the next, The two strings are converted to the minimum string of the dictionary order (the minimum notation of the circular section) and then the two minimum representations of A and B are test instructions.

Later thought for a half-day why can not divide into 4^ (log2 (n)) complexity? The reason is this: we construct the string according to this complexity. First, if you want to compare the AL and AR, BL and BR comparison, and the comparison of Al and Br, AR and BL are also compared, you must meet the equivalence of Al and BL, AR and BR is not equivalent, and the equivalent of Al and BR, so that the AR and BL to ensure that the comparison. However, when we compare the Al and BL, and then divide, set Al divided into All,alr,bl divided into BLL,BLR, to let it compare 4 times, then there is all and the BLL equivalent, ALR and BlR is not equivalent, ALR and the BLL equivalent, but because of this case, the AL and BL are equivalent, So there must be ALR and BLL equivalents. We simply write

all = BLL

ALR! = BlR

ALR = BLL

all = BlR

However, these 4 equations can be rolled out all = BLL = ALR = BlR, i.e. 4 substrings can be equivalent in any one, contradicting the second equation. This means that a string can not be constructed to achieve 4^ (LOG2 (n)) of complexity. In fact, in many cases recursion is returned three times or even two times. Therefore, the efficiency of division is also very high. Of course, the complexity of the minimum notation is O (n*log (n)), which is sure to be over. In fact, the idea of divided treatment is just 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 translation (char *x, int l, int r)//turns the original string into a string with the smallest dictionary 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 (a), LenB = strlen (b);    Translation (A, 0, lena-1);    Translation (b, 0, lenb-1); printf ("%s\n", STRCMP (A, b) = = 0? "YES": "NO");}    int main () {while (scanf ("%s%s", A, b)! = EOF) solve (); return 0;}



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

Codeforces 559B equivalent Strings equivalent string

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.