字符相等(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 ...
随机推荐
- 1.1. chromium源代码分析 - chromiumframe - 介绍
本人能力有效,面对chromium庞大的代码就头大.还是先由前辈的chromiumFrame入手. 1. chromeFrame概貌 chromiumFrame是前辈的心血之作,以最小化的方式抽出ch ...
- django1.4.5无法安装MySQLdb1.2.3
解决办法是: yum install python-devel mysql-devel zlib-devel openssl-devel 然后再build.install
- css布局: 两栏 自适应高度
只使用css实现 有两种方式, 一种是通过相对定位,再绝对定位获取父亲元素的高度, 一种是通过margin-bottom:-999em;padding-bottom: 999em; 父亲元素超出隐藏 ...
- Oracle系统视图
VIEW TABLES 1.DBA_TABLES --查看系统里所有表的信息,只有DBA权限用户才可查看 SELECT * FROM DBA_TABLES WHERE OWNER='HR' AND T ...
- python mysql多条插入
程序的目的是把文本里面的数据存储到数据库中,原来的思路是读一条,插入一条,结果就是时间长的不得了...18万条的数据,真是慢. 后来的想法是把所有的记录都读到一个list里,结果是mysql奔溃go ...
- 在windows系统中安装hadoop
1.安装Cygwin 从http://www.cygwin.com/ 下载cygwin的setup.exe,双击运行: 选择从Internet安装: 设置安装目录: 设置安装包目录: 设置“Inter ...
- Kapit控件方法笔记
r.kapit.visualizer.renderers.DefaultItemRenderer //整个节点添加click处理函数对象类型 fr.kapit.visualizer.controls. ...
- Microsoft Deployment Toolkit 2013 Preview Release Now Available
MDT 2013 provides a common console with comprehensive tools and guidance for every organizational ro ...
- android AChartEngine源代码
昨天翻自己曾经下过的apache开源project项目,看到一个AChartEnginee看了一下自带的Demo才意识到这个东西的强大.立刻想把源代码down一份,在CSDN上有人挂5分让人下载,实在 ...
- Java多线程之synchronized(五)
上篇介绍了用synchronized修饰static方式来实现“Class 锁”,今天要介绍另一种实现方式,synchronized(class)代码块,写法不一样但是作用是一样的.下面我附上一段代码 ...