判断字符相等

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:

  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

Sample Output

 

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

题意:

两个字符串a,b等长称为相等。

1.a,b是直接相等的

2.a,b可以分为相同大小的两半,a1.a2.b1.b2,下面有一个是正确的:

1.a1=b1  且 a2=b2

2.a1=b2且a2=b1

判断两个字符串是否相等。

分析:

1.DFS(深度优先搜索)

2.字符串长度为奇数时直接比较两个字符串相不相等;字符串长度为偶数时,平分字符串,比较,如果不相等继续分,一直到字符串长度为奇数。

代码:

   #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=; char a[maxn],b[maxn]; bool dfs(char *p1,char *p2,int len)
{
if(!strncmp(p1,p2,len))
return true;
if(len%) //判断字符串长度是奇是偶
return false;
int n=len/; //将字符串平均分为两部分
if(dfs(p1,p2+n,n)&&dfs(p1+n,p2,n))
return true;
if(dfs(p1,p2,n)&&dfs(p1+n,p2+n,n)) //字符串比较
return true;
return false; } int main()
{
scanf("%s%s",a,b);
printf("%s\n",dfs(a,b,strlen(a))?"YES":"NO");
return ;
}

又是看了别人的代码。好想自己写出来。

字符相等(E - 暴力求解、DFS)的更多相关文章

  1. CodeForces 339C Xenia and Weights(暴力求解DFS)

    题意:给定 1-10的某几种砝码,给定的每种有无穷多个,然后放 m 个在天平上,要满足,相邻的两次放的砝码不能是同一种,然后是在天平两端轮流放,并且放在哪一个托盘上,那么天平必须是往哪边偏. 析:这个 ...

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

    油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...

  3. <字符串匹配>KMP算法为何比暴力求解的时间复杂度更低?

    str表示文本串,m表示模式串; str[i+j] 和 m[j] 是正在进行匹配的字符; KMP的时间复杂度是O(m+n)  ,  暴力求解的时间复杂度是O(m*n) KMP利用了B[0:j]和A[i ...

  4. 逆向暴力求解 538.D Weird Chess

    11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ...

  5. 隐型马尔科夫模型(HMM)向前算法实例讲解(暴力求解+代码实现)---盒子模型

    先来解释一下HMM的向前算法: 前向后向算法是前向算法和后向算法的统称,这两个算法都可以用来求HMM观测序列的概率.我们先来看看前向算法是如何求解这个问题的. 前向算法本质上属于动态规划的算法,也就是 ...

  6. BestCoder Round #79 (div.2)-jrMz and angles,,暴力求解~

    jrMz and angle       Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Other ...

  7. hdu6570Wave (暴力求解)

    Problem Description Avin is studying series. A series is called "wave" if the following co ...

  8. 暴力求解——UVA 572(简单的dfs)

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  9. zoj 1008 暴力枚举求解dfs+优化

    /* 现将相同的合并计数. 再枚举判断是否符合当cou==n*n是符合就退出 */ #include<stdio.h> #include<string.h> #define N ...

随机推荐

  1. 在Mac pro上如何配置adb命令?

    在Mac pro上如何将Android SDK的adb命令添加到环境变量中,这里将进行说明! 方法/步骤 1 启动终端,可以在Spotlight中搜索“终端” 2 进入当前用户的HOME目录,命令如下 ...

  2. Android应用开发基础篇(11)-----ViewFlipper

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/01/2376067.html 一.概述 ViewFlipper这个部件是用来实现多页显示的,多页 ...

  3. [NOIP2013提高组] CODEVS 3287 火车运输(MST+LCA)

    一开始觉得是网络流..仔细一看应该是最短路,再看数据范围..呵呵不会写...这道题是最大生成树+最近公共祖先.第一次写..表示各种乱.. 因为要求运输货物质量最大,所以路径一定是在最大生成树上的.然后 ...

  4. Windows下的PHP开发环境搭建——PHP线程安全与非线程安全、Apache版本选择,及详解五种运行模式。

    今天为在Windows下建立PHP开发环境,在考虑下载何种PHP版本时,遭遇一些让我困惑的情况,为了解决这些困惑,不出意料地牵扯出更多让我困惑的问题. 为了将这些困惑一网打尽,我花了一下午加一晚上的时 ...

  5. PHP学习笔记7-JSON数据操作

    JSON,全称是JavaScript Object Notation.它是基于JavaScript编程语言ECMA-262 3rd Edition-December 1999标准的一种轻量级的数据交换 ...

  6. .net mvc Authorization Filter,Exception Filter与Action Filter

    一:知识点部分 权限是做网页经常要涉及到的一个知识点,在使用MVC做权限设计时需要先了解以下知识: MVC中Url的执行是按照Controller->Action->View页面,但是我们 ...

  7. IPhone多视图切换

    处理IPhone多个view切换是我们常遇到的问题,接下来有个比较实用的方法: 而且还附有创建空项目,内存告急处理和动画效果的实现! 具体步骤: 1.创建一个空的项目,然后添加一个ViewContro ...

  8. android AChartEngine源代码

    昨天翻自己曾经下过的apache开源project项目,看到一个AChartEnginee看了一下自带的Demo才意识到这个东西的强大.立刻想把源代码down一份,在CSDN上有人挂5分让人下载,实在 ...

  9. ORACLE分科目统计每科前三名的学生的语句

    有个成绩表 score(student_no,Subject_no,Score)分别为学号,课程号,成绩.我想用语句查询出每科的前三名学生的学号,请各位高手教教小弟 1.创建测试语句:create t ...

  10. SQL Server调试常用参数

    SQL Server 清除缓存: DBCC DROPCLEANBUFFERS 从缓冲池中删除所有清除缓冲区. DBCC FREEPROCCACHE 从过程缓存中删除所有元素. DBCC FREESYS ...