暴力,DFS,比较字符串

题意:

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

思路分析:

使用DFS。

明白几点:

A:字符串长度为奇数时直接比较两个字符串相不相等

B:字符串长度为偶数时,平分字符串,像题目的案例那样比较。如果不相等继续分,一直到字符串长度为奇数时,看A。

1、首先比较两个字符串是否完全一样,用strncmp函数。相等就直接输出YES

2、不相等就判断字符串的长度,分偶数跟奇数,如A跟B所述

3、拆分好就比较,具体见代码

源代码:

 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 using namespace std;
 5 const int m = 200010;
 6 char s1[m], s2[m];
 7 bool dfs(char *a,char *b,int n)
 8 {
 9     if (strncmp(a, b, n) == 0)              //比较字符串
10         return true;
11     if (n % 2 != 0)                          //判断字符串长度为偶数还是奇数
12         return false;
13     int len = n / 2;                        //平分字符串
14
15     if (dfs(a, b + len, len) && dfs(a + len, b, len))
16         return true;
17     if (dfs(a, b, len) && dfs(a + len, b + len, len))    //拆分后的字符串比较
18         return true;
19     return false;
20 }
21 int main()
22 {
23       cin >> s1;
24       cin >> s2;
25
26       cout <<( dfs(s1, s2, strlen(s1)) ? "YES" : "NO") << endl;
27
28     return 0;
29 }

心得:

按照自己以前的思路写,就是一大堆循环,然后自己也被弄晕了。这个题借用了别人的写法,觉得写得不错,简洁易懂,应该多学习,加油~~~

但是提交时下面两句换一下就会超时。。。至今还没弄明白。。。。

1 if (dfs(a, b + len, len) && dfs(a + len, b, len))
2         return true;
3     if (dfs(a, b, len) && dfs(a + len, b + len, len))    //拆分后的字符串比较
4         return true;
时间: 2024-12-15 01:28:05

暴力,DFS,比较字符串的相关文章

A. The Fault in Our Cubes 暴力dfs

http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行,就会早早退出. 这样写起来比较好写. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <

hdu 5024 Wang Xifeng&#39;s Little Plot【暴力dfs,剪枝】

2014年广州网络赛的C题,也是水题.要你在一个地图中找出一条最长的路,这条路要保证最多只能有一个拐角,且只能为90度 我们直接用深搜,枚举每个起点,每个方向进行dfs,再加上剪枝. 但是如果直接写的话,那一定会特别麻烦,再加上方向这一属性也是我们需要考虑的方面,我们将从别的地方到当前点的方向编一个号:往右为0,如下图顺时针顺序编号 (往右下方向为1,往下为2......以此类推) 我们知道了当前点的坐标,来到当前点的方向,以及到目前有没有拐弯,这几个属性之后,就可以记忆化搜索了,用一个四维数组

Codeforces Round #359 (Div. 2) C. Robbers&#39; watch (暴力DFS)

题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b < m 且a的7进制和n-1的7进制位数相同 且b的7进制和m-1的7进制位数相同,还有a和b的7进制上的每位上的数各不相同. 看懂题目,就很简单了,先判断a和b的7进制位数是否超过7,不超过的话就dfs暴力枚举计算就可以了. 1 //#pragma comment(linker, "/STACK

hihoCoder 1185 连通性&#183;三(Tarjan缩点+暴力DFS)

#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出去,就拜托小Hi和小Ho忙帮放牧. 约翰家一共有N个草场,每个草场有容量为W[i]的牧草,N个草场之间有M条单向的路径. 小Hi和小Ho需要将牛羊群赶到草场上,当他们吃完一个草场牧草后,继续前往其他草场.当没有可以到达的草场或是能够到达的草场都已经被吃光了之后,小hi和小Ho就把牛羊群赶回家. 一开

Strange Country II 暴力dfs

这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cmath> #include <stack> #

ZOJ Seven-Segment Display 暴力dfs + 剪枝

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3954 0 = on     1 = off A seven segment code of permutation p is a set of seven segment code derived from the standard code by rearranging the bits into the order indicated by p. For exampl

UVa 818Cutting Chains (暴力dfs+位运算+二进制法)

题意:有 n 个圆环,其中有一些已经扣在一起了,现在要打开尽量少的环,使所有的环可以组成一条链. 析:刚开始看的时候,确实是不会啊....现在有点思路,但是还是差一点,方法也不够好,最后还是参考了网上的题解,大神们的代码就是不一样, 但还是看了好久才看懂.首先是用二进制法进行暴力,因为 n 最大才是15,不会超时的,然后就是在暴力时判断打开这些环时,剩下的是不是还存在环, 如果存在那么不是不行的,然后再判断是不是有的环有两个分支以上,因为一个环如果成链那么最多只有两个分支,所以多于两个的也是不对

HDU 5113 Black And White(暴力dfs+减枝)

题目大意:给你一个n×m的矩阵,然后给你k种颜色,每种颜色有x种,所有的个数加起来恰好为n×m个.问你让你对这个矩阵进行染色问你,能不能把所有的小方格都染色,而且相邻两个颜色不同. 思路:一开始想的是构造,先按照个数进行排序,枚举每一个位置,贪心的策略先放多的,如果可以全部放下就输出YES,以及存贮的方案,否则输出NO,但是有bug,一直不对... 正解:dfs暴力枚举每一个点,裸的话需要25!,显然会超时,需要先排个序用构造的策略,让多的先放这样可以减枝.然后再dfs就可以了. Black A

洛谷OJ 1433 吃奶酪 暴力dfs(最优性cut)

https://www.luogu.org/problem/show?pid=1433 题意:起点为(0,0) 有n个点 求出起点出发经过n个点的最小距离//15个点 暴力 + 最优性cut(sum+还剩的点*最小距离>=ans 则stop) 水过... #include <bits/stdc++.h> using namespace std; typedef pair<int,double> pii; const int N=2e3+20; const double inf

hdu 4499 Cannon 暴力dfs搜索

Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 589    Accepted Submission(s): 338 Problem Description In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move