Codeforces Round #313 (Div. 1) B. Equivalent Strings

Source: Internet
Author: User
Tags comparison
equivalent Strings problem ' s link:http://codeforces.com/contest/559/problem/b

Mean:

Given two equal-length string s1,s2, the judgment is equivalent.

The meaning of equivalence is:

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 l1,r1 and L2,r2, where L1 and L2 are equivalent and R1 and R2 equivalent, or L1 and R2 equivalent and L2 and R1 equivalent.

Analyse:

Just follow the test instructions simulation to write a recursive division of the line.
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 be divided into 4^ (log2 (n)) of the complexity of it.
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.

Time complexity:o (N*LOGN)

Source Code:/*
* This code was made by crazyacking
* verdict:accepted
* Submission date:2015-07-22-22.45
* TIME:0MS
* MEMORY:137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL Long Long
#define ULL unsigned long long
using namespace Std;
#define NN 200000+50
char A [nn], B [NN];
int cmp (char x [], char y [], int len)
{
for (int i = 0; i < len; + + i)
if (x [i] < y [i]) return-1;
else if (x [i] > y [i]) return 1;
}

void work (int len, char x [])
{
if (len% 2 = = 1) return;
Work (LEN/2, x);
Work (LEN/2, x + LEN/2);
if (CMP (x, x + LEN/2, LEN/2) > 0)
for (int i = 0; i < LEN/2; + + i)
Swap (x [i], x [i + LEN/2]);
}
int main ()
{
Ios_base:: Sync_with_stdio (false);
Cin. Tie (0);
scanf ("%s%s", A, B);
int len = strlen (A);
Work (Len, B);
Work (Len, A);
if (strcmp (A, B) = = 0) puts ("YES");
Else puts ("NO");
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.