ACM Judges string "equal"

Source: Internet
Author: User

Description

Today on a lecture about strings Gerald learned a new definition of string equivalency. Strings a and b of equal length is calledequivalent in one of the of the cases:

  1. They is equal.
  2. If We split string a  into both halves of the same Size  a 1 and  a 2, and String  b  into both halves of the same size   b 1 and  b 2, then one of the following is correct: /sub>
      1. a 1 is equivalent to b1, and a2 are equivalent to b2
      2. a 1 is equivalent to b2, and a2 are equivalent to b1

As a home task, the teacher gave the strings to his students and asked to determine if they is equivalent.

Gerald have already completed this home task. Now it ' s your turn!

Input

The first and lines of the input contain the strings given by the teacher. Each of them have the length from 1 to and consists of lowercase 中文版 letters. The strings has the same length.

Output

Print "YES" (without the quotes), if these, strings is equivalent, and "NO" (without the quotes) otherwise.

Sample Input

Input
Aaba
Abaa
Output
YES
Input
Aabb
Abab
Output
NO

Hint

In the first sample you should split the first string into strings "aa" and "ba ", the second One-into strings" ab "and" aa". "aa" is equivalent to "AA"; "ab" is equivalent to "ba" as "ab" = "a" + "b", "ba" = "b" + "a".

In the second sample the first string can is splitted into strings "AA" and "BB", which is equivalent only to them Selves. That's why string "Aabb" was equivalent only to itself and tostring "Bbaa".

Problem Solving Ideas:

The main idea of the topic is to give two strings to determine if the two strings are "equal". The condition of "equality" is that a b two strings are split into A1,a2,b1,b2, A1=B1 and A2=b2 or A1=B2 and a2=b1 such that a B two strings are "equal". So we can first determine whether the two strings are equal, if so, the two strings are "equal", and if the length of the string is odd, then it is definitely not waiting. Constant recursive judgment.

Program code:

#include <iostream>#include<string>#include<cstring>using namespacestd;Const intm =200010;CharS1[m], s2[m];BOOLDfsChar*a,Char*b,intN) {   if(STRNCMP (A, b, n) = =0)//Comparing Strings       return true; if(n%2!=0)//determine whether the string length is even or odd       return false; intLen = n/2;//Split String      if(Dfs (A, B + len, len) && Dfs (A +Len, B, Len)) return true; if(Dfs (A, B, Len) && Dfs (A + len, B + len, Len))//string comparison after splitting       return true; return false;}intMain () {CIN>>S1; CIN>>S2; cout<< (Dfs (S1, S2, strlen (S1))?"YES":"NO") <<Endl; return 0;

ACM Judges string "equal"

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.