Codeforces Round #313 D. Equivalent Strings(DFS)
2 seconds
256 megabytes
standard input
standard output
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:
- 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
- a1 is
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!
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.
Print "YES" (without the quotes), if these two strings are equivalent, and "NO"
(without the quotes) otherwise.
aaba
abaa
YES
aabb
abab
NO
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".
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> using namespace std; char a[200010],b[200010]; int DFS(char *a,char *b,int l)
{
if(strncmp(a,b,l) == 0)
{
return 1;
}
if(l%2)
{
return 0;
}
int p = l / 2;
if((DFS(a,b+p,p) && DFS(a+p,b,p)) || (DFS(a,b,p) && DFS(a+p,b+p,p)))
{
return 1;
}
return 0;
} int main()
{
while(scanf("%s",a)!=EOF)
{
scanf("%s",b);
int len = strlen(a);
int pk = DFS(a,b,len);
if(pk == 1)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}
Codeforces Round #313 D. Equivalent Strings(DFS)的更多相关文章
- Codeforces Round 662 赛后解题报告(A-E2)
Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...
- Codeforces Round #306 (Div. 2) ABCDE(构造)
A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
- CodeForces 165C Another Problem on Strings(组合)
A string is binary, if it consists only of characters "0" and "1". String v is a ...
- Codeforces Round #527 (Div. 3)F(DFS,DP)
#include<bits/stdc++.h>using namespace std;const int N=200005;int n,A[N];long long Mx,tot,S[N] ...
- Codeforces Round #267 (Div. 2)D(DFS+单词hash+简单DP)
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【Codeforces 723D】Lakes in Berland (dfs)
海洋包围的小岛,岛内的有湖,'.'代表水,'*'代表陆地,给出的n*m的地图里至少有k个湖,求填掉面积尽量少的水,使得湖的数量正好为k. dfs找出所有水联通块,判断一下是否是湖(海水区非湖).将湖按 ...
- codeforces 805 E. Ice cream coloring(dfs)
题目链接:http://codeforces.com/contest/805/problem/E 题意:你有n个节点,这个n个节点构成一棵树.每个节点拥有有si个类型的ice,同一个节点的ice互相连 ...
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
- Codeforces Round #309 (Div. 1) A(组合数学)
题目:http://codeforces.com/contest/553/problem/A 题意:给你k个颜色的球,下面k行代表每个颜色的球有多少个,规定第i种颜色的球的最后一个在第i-1种颜色的球 ...
随机推荐
- 安卓测试之ADB命令
什么是ADB: adb的全称为Android Debug Bridge,就是起到调试桥的作用.借助adb工具,我们可以管理设备或手机模拟器的状态.还可以进行很多手机操作,如安装软件.系统升级.运行sh ...
- vue实现前端导出excel表格
1.在src目录下创建一个文件(vendor)进入Blob.js和Export2Excel.js 2.npm install -S file-saver 用来生成文件的web应用程序 3.npm in ...
- GetLastError()返回值大全
[0]-操作成功完成.[1]-功能错误.[2]-系统找不到指定的文件.[3]-系统找不到指定的路径.[4]-系统无法打开文件.[5]-拒绝访问.[6]-句柄无效.[7]-存储控制块被损坏.[8]-存储 ...
- 判断IP是否为爬虫IP
方法一: 通过国外网站验证:http://bot.myip.ms/123.125.71.12 返回结果: IP/Domain - 123.125.71.12: Baidu Bot on this ...
- JavaEE各种Javadoc的下载
刚才突然发现,很多servlet的api都不熟悉,但是又没有给eclipse导入api,就google了一下 关键字,servlet api / doc 或者java ee api / doc 注意版 ...
- java 读取execl文件
java 中读取execl文件是必要功能,下面说下几种读取方式 1.jxl (支持2003 不支持 2007 貌似最新版支持) /** * 规则设置的模板导入 * @param fi ...
- Mariadb源代码编译过程
从微博上看到有人提及Mariadb,搜索了一下.找到地址https://mariadb.org/,这是mysql的一个分支,由原作者维护.意在与oracle分庭抗礼,避免oracle将来毕源. 眼下版 ...
- Quartz 与 Spring集成
http://www.cnblogs.com/pigwing/archive/2011/07/12/2104002.html http://blog.arganzheng.me/posts/quart ...
- MySQL做为手动开启事务用法
START TRANSACTION;INSERT INTO `t1` (t, t1) VALUES('124', NOW());ROLLBACK;COMMIT;
- Java命令学习系列(二)——Jstack
Java命令学习系列(二)——Jstack 2015-04-18 分类:Java 阅读(512) 评论(0) jstack是java虚拟机自带的一种堆栈跟踪工具. 功能 jstack用于生成java虚 ...