3thweek——E.暴力求解+DFS

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

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 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".

分析步骤:

1.先判断两串字符串长度为奇数还是偶数,奇数则要遍历全部的两个字符串,字符和顺序都相同才能输出YES,否则输出NO。

2.如果是偶数,两个字符串完全相同就直接输出YES,否则将两个字符串各切成两个子字符串再比较。

代码如下:

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 const int maxn=200005;
 5 char a[maxn],b[maxn];
 6
 7 int i,flag=0;
 8 int compare(char* a1,char* b1,int l)  //自定义比较函数
 9 {
10     for(i=0;i<l;i++)
11     {
12         if(a1[i]!=b1[i])
13         {
14             return 0;
15         }
16     }
17     flag=1;
18     return flag;
19 }
20
21
22 int fun(char* a1,char* b1,int l)
23 {
24     int k=0;
25     if(compare(a1,b1,l))
26            k=1;
27     if(l%2==1)
28     {
29         if(k==1)
30            return 1;
31         else
32             return 0;
33     }
34     else
35     {
36         if(k==1)
37             return 1;
38     else
39     {
40             l=l/2;
41           if( (fun(a1,b1,l)  && fun(a1+l,b1+l,l)) || (fun(a1,b1+l,l) && fun(a1+l,b1,l)) )
42             return 1;
43           else
44               return 0;
45     }
46     }
47 }
48
49 int main()
50 {
51     cin>>a;
52     cin>>b;
53     int len=strlen(a);
54     if(fun(a,b,len))
55        cout<<"YES"<<endl;
56     else
57         cout<<"NO"<<endl;
58 return 0;
59 }
时间: 2024-08-04 15:20:19

3thweek——E.暴力求解+DFS的相关文章

POJ 1562(L - 暴力求解、DFS)

油田问题(L - 暴力求解.DFS) 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 numerou

第四章 分治策略 4.1 最大子数组问题 (暴力求解算法)

/** * 最大子数组的暴力求解算法,复杂度为o(n2) * @param n * @return */ static MaxSubarray findMaxSubarraySlower(int[] n) { long tempSum = 0; int left = 0; int right = 0; long sum = Long.MIN_VALUE; for (int i = 0; i < n.length; i++) { for (int j = i; j < n.length; j++

UVA 152-Tree&#39;s a Crowd(暴力求解三维坐标求最短距离)

Tree's a Crowd Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  Tree's a Crowd  Dr William Larch, noted plant psychologist and inventor of the phrase ``Think like a tree--Think Fig'' has invented a new

求解最大子数组问题 -- 暴力求解 和 分治法求解

/*------------------ 求解最大子数组问题 --------------- 最大子数组,就是求解一个数组的所有元素的各种组合中,和最大的 那个子数组.在这种情况下,如果元素值全部非负,那么最大子数组当然 是所有元素.但是如果有负值的元素存在,那么久需要找到一个由数组中 连续几个元素组成的子数组并且其元素值之和最大. */ #include <iostream> using namespace std; struct ArrayStruct { ArrayStruct(int

BZOJ 1054题解 BFS暴力求解

BZOJ 1054题解 BFS暴力求解 1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1884  Solved: 1033[Submit][Status][Discuss] Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动 时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初始的玩具状态移 动到某人

HDU 1431 素数回文【暴力求解】

/* 题目大意:找一个范围内的所有素数回文数 解题思路:打一个表将1亿以内所有的素数回文数找出来,大概有780个这样子 关键点:暴力求解 解题人:lingnichong 解题时间:2014-08-29 12:02:55 解题体会:如果按一般方法打个素数表,很容易超内存(MLE),所以就先将所有的素数回文全部算出来,再在这个数组里面找在题上那个范围的所有素数回文数 */ 素数回文 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 655

字符相等(E - 暴力求解、DFS)

判断字符相等 Description Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: They are equal. If we split string a into two halves of the

HDU 4770 Lights Against Dudely 暴力枚举+dfs

又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ czy Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1360    Accepted Subm

UVa 817 According to Bartjens (暴力,DFS)

题意:给出一个数字组成的字符串,然后在字符串内添加三种运算符号 * + - ,要求输出所有添加运算符并运算后结果等于2000的式子. 所有数字不能有前导0, 且式子必须是合法的. 析:这个题很明显的暴力,因为最长才9位数字,也就是最多有8个位置位置可能插符号,当然实际并没有那么多,所以直接暴力就行,也不用优化,直接暴就行. 就是DFS,在每个位置考虑四种情况,*,+,-,或者不放,最后再一个一个的判断是不是等于2000就好,注意这个题有一个坑,我也不知道是哪个数据, 也没有想到,就是一个没有用运