HUAS Summer Trainning #3~E

Description

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

  1. They are equal.
  2. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2, then one of the following is correct:
    1. a1 is equivalent to b1, and a2 is equivalent to b2
    2. a1 is equivalent to b2, and a2 is equivalent to b1

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

Gerald has already completed this home task. Now it‘s your turn!

Input

The first two lines of the input contain two strings given by the teacher. Each of them has the length from 1 to 200 000 and consists of lowercase English letters. The strings have the same length.

Output

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

Sample Input

Input

aabaabaa

Output

YES

Input

aabbabab

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 be splitted into strings "aa" and "bb", that are equivalent only to themselves. That‘s why string "aabb" is equivalent only to itself and to string "bbaa".

解题思路:这个题目用暴力、DFS来求解最好,为了提高效率,首先就要判断输入的字符串是奇数还是偶数,字符串长度为奇数时直接比较两个字符串相不相等,字符串长度为偶数时,平分字符串,像题目的案例那样比较。如果不相等继续分,一直到字符串长度为奇数时,再看字符串长度为奇数时直接比较两个字符串相不相等,首先比较两个字符串是否完全一样,用strncmp函数。相等就直接输出YES,不相等就判断字符串的长度,分偶数跟奇数,拆分好就比较。运用这种方法更能够提高效率,去掉一些不必要的代码,减少时间上的错误。

程序代码:

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int m = 200010;
char s1[m], s2[m];
bool dfs(char *a,char *b,int n)
{
    if (strncmp(a, b, n) == 0)              //比较字符串
        return true;
    if (n % 2 != 0)                          //判断字符串长度为偶数还是奇数
        return false;
    int len = n / 2;                        //平分字符串
   
    if (dfs(a, b + len, len) && dfs(a + len, b, len))
        return true;                                   
    if (dfs(a, b, len) && dfs(a + len, b + len, len))    //拆分后的字符串比较
        return true;
    return false;
}
int main()
{
      cin >> s1;
      cin >> s2;
    
      cout <<( dfs(s1, s2, strlen(s1)) ? "YES" : "NO") << endl;
   
    return 0;
}

时间: 2024-10-09 10:03:58

HUAS Summer Trainning #3~E的相关文章

2015 HUAS Summer Trainning #6~H

Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识.  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数.  给定一个区间,你能计算出这个区间内有多少个美素数吗? Input 第一行输入一个正整数T,表示总共有T组数据(T <= 10000). 接下来共T行,每行输入两个整数L,R(1<= L <= R <= 10

2015 HUAS Summer Trainning #4 B

Description Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so calledscribers. The scriber had been given a book and after several months he finished its copy. One of

2015 HUAS Summer Trainning #4 C

My birthday is coming up and traditionally I’mserving pie. Not just one pie, no, I have a numberN of them, of various tastes and of various sizes. Fof my friends are coming to my party and each ofthem gets a piece of pie. This should be one pieceof o

2015 HUAS Summer Trainning #4 D

Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the minimalamount of them, such they would completely cover the segment [0, M]. InputThe first line is the number of test cases, followed by a blank line.Eac

2015 HUAS Summer Trainning #5~E

Description The world financial crisis is quite a subject. Some people are more relaxed while others are quite anxious. John is one of them. He is very concerned about the evolution of the stock exchange. He follows stock prices every day looking for

2015 HUAS Summer Trainning #4 A

Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input seque

HUAS Summer Trainning #3~L

Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It

HUAS Summer Trainning #3~M

Description 我们知道,在编程中,我们时常需要考虑到时间复杂度,特别是对于循环的部分.例如, 如果代码中出现 for(i=1;i<=n;i++) OP ; 那么做了n次OP运算,如果代码中出现 fori=1;i<=n; i++)   for(j=i+1;j<=n; j++) OP; 那么做了n*(n-1)/2 次OP 操作. 现在给你已知有m层for循环操作,且每次for中变量的起始值是上一个变量的起始值+1(第一个变量的起始值是1),终止值都是一个输入的n,问最后OP有总共多

2015 HUAS Summer Trainning #5~N

Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … The bone collector had a big bag with a volume of V ,and