字符相等(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 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:
- a1 is equivalent to b1, and a2 is equivalent to b2
- 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
aaba abaa
YES
aabb abab
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)的更多相关文章
- CodeForces 339C Xenia and Weights(暴力求解DFS)
题意:给定 1-10的某几种砝码,给定的每种有无穷多个,然后放 m 个在天平上,要满足,相邻的两次放的砝码不能是同一种,然后是在天平两端轮流放,并且放在哪一个托盘上,那么天平必须是往哪边偏. 析:这个 ...
- POJ 1562(L - 暴力求解、DFS)
油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...
- <字符串匹配>KMP算法为何比暴力求解的时间复杂度更低?
str表示文本串,m表示模式串; str[i+j] 和 m[j] 是正在进行匹配的字符; KMP的时间复杂度是O(m+n) , 暴力求解的时间复杂度是O(m*n) KMP利用了B[0:j]和A[i ...
- 逆向暴力求解 538.D Weird Chess
11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ...
- 隐型马尔科夫模型(HMM)向前算法实例讲解(暴力求解+代码实现)---盒子模型
先来解释一下HMM的向前算法: 前向后向算法是前向算法和后向算法的统称,这两个算法都可以用来求HMM观测序列的概率.我们先来看看前向算法是如何求解这个问题的. 前向算法本质上属于动态规划的算法,也就是 ...
- 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 ...
- hdu6570Wave (暴力求解)
Problem Description Avin is studying series. A series is called "wave" if the following co ...
- 暴力求解——UVA 572(简单的dfs)
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- zoj 1008 暴力枚举求解dfs+优化
/* 现将相同的合并计数. 再枚举判断是否符合当cou==n*n是符合就退出 */ #include<stdio.h> #include<string.h> #define N ...
随机推荐
- ASP.NET产生随机验证码
效果图:(Flowing) 1.项目中新建用于存储(位图)图片文件夹 图解: 2.前台可以添加一ASP.NET控件或其他任意用来展示图片标签等(如下) <div> <asp:Imag ...
- SVN 无法连接主机:由于目标计算机积极拒绝,无法连接
问题:使用追溯功能时因为时间太长,所以强行关闭了SVN;当再次连接的时候就发现不能连接到SVN了,错误消息: 无法连接主机:由于目标计算机积极拒绝,无法连接 解决:重启一下服务器的SVN 服务就可以了
- ForeignKey.on_delete
当由一个 ForeignKey 引用的对象被删除,默认情况下,Django模拟SQL的 ON DELETE CASCADE 来删除对象的 ForeignKey 关系.这样可以覆盖指定的 on_dele ...
- 射频识别技术漫谈(18)——Mifare Desfire
Mifare DESFire(MF3 IC D40/D41,本文以D40为例)遵守14443 TypeA协议,卡内的数据以文件形式存储,所以有人认为它是准CPU卡,主要用于安全性要求较高的非接触式领 ...
- Qt的目录依赖问题----怎样生成一个绿色的Qt软件包
Qt的目录依赖问题----怎样生成一个绿色的Qt软件包 一.核心问题: 如果将编译好的Qt文件拷贝到任何目录下,作为一个绿色软件库来使用? 二.原理讨论 由于Qt在编译的时候将安装路径硬编码到了库文件 ...
- 通往WinDbg的捷径
通往WinDbg的捷径(一) 原文:http://www.debuginfo.com/articles/easywindbg.html译者:arhat时间:2006年4月13日关键词:CDB WinD ...
- java学习之坦克大战游戏
总结:由于这几天快过年比较忙然后没怎么写,写代码途中一些经验总结现在给忘记了.这次的小项目感觉比上次写的思路清楚了点.没有之前第一次写那么逻辑混乱,结构也搞的比之前的要好,添加功能比较容易.学习了之前 ...
- Flex疑难小杂症
本文主要解决Flex中一些小问题,收集一些小技巧(来自网络及个人经验) flex自动换行问题 有时候由于label .button等控件中需要用到text属性显示出文本,文本太长就涉及到换行问题,解 ...
- Mysql 创建联合主键
Mysql 创建联合主键2008年01月11日 星期五 下午 5:21使用primary key (fieldlist) 比如: create table mytable ( ...
- swiper,animate使用方法
1.先链接css和js文件 <link rel="stylesheet" type="text/css" href="css/swiper-3. ...