判断字符相等

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. Android应用开发基础篇(15)-----URL(获取指定网址里的图片)

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/10/2389190.html 一.概述 URL,说白了就是一个网络地址(网址),通常一个网址里包 ...

  2. JavaSE_ 多线程 总目录(23~24)

    JavaSE学习总结第23天_多线程123.01 多线程程序的引入23.02 进程概述及多进程的意义23.03 线程概述及多线程的意义23.04 并行和并发的区别23.05 Java程序运行原理和JV ...

  3. Mysql数据库中 ,涉及事物,循环添加数据

    ),),   ) BEGIN declare cnt ; declare i ; declare growthid ; DECLARE t_error INTEGER DEFAULT ;  DECLA ...

  4. Spark学习资料

    1. 倾情大奉送--Spark入门实战系列 2. Spark GraphX: http://blog.csdn.net/bluejoe2000/article/details/44308167

  5. CI 模板解析器类

    模板解析器类可以解析你的视图文件中的伪变量.它可以解析简单的变量或者以变量作为标签的结构.如果你以前没有用过模板引擎,那么伪变量如下所示: <html><head><ti ...

  6. IPTABLES 映射问题

    今天要做一个新的映射:将内网的一个8090口映射到外网的8087口. 在 /ETC/RC.LOCAL中最后插入: iptables -t nat -A PREROUTING -d outIP -p t ...

  7. 1148 - Mad Counting(数学)

    1148 - Mad Counting   PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB M ...

  8. 字符串分割函数Demo

    #include <stdio.h> int getLength(char *string); int main(int argc, char **argv){ char str[12] ...

  9. Usaco 2.3 Zero Sums(回溯DFS)--暴搜

    Zero SumConsider the sequence of digits from 1 through N (where N=9) in increasing order: 1 2 3 ... ...

  10. 关于Spring的BeanPostProcessor

    BeanPostProcessor接口作用是:如果我们需要在Spring容器完成Bean的实例化.配置和其他的初始化前后添加一些自己的逻辑处理,我们就可以定义一个或者多个BeanPostProcess ...