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种颜色的球 ...
随机推荐
- jQuery-File-Upload文件上传
http://blueimp.github.io/jQuery-File-Upload/index.html
- python 高阶函数 map lambda filter等
map 描述 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表. ...
- 一款基jquery超炫的动画导航菜单
今天给大家分享一款基jquery超炫的动画导航菜单.这款导航菜单,初始时页面中间一个按钮,单击按钮,菜单从左侧飞入页中.再次单击按钮,导航飞入左侧消息.动画效果很非常炫.一起看下效果图: 在线预览 ...
- cocos2dx遇到的坑1
记录下在cocos2dx 2.x时代遇到的问题 1.节点的观念,用节点来管理 2.pushscene popscene 和replacewithscene runwithscene对应 3.lua里释 ...
- JavaScrip——简单练习(抓错误信息,for循环,日期)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JVM监控添加参数
-Dcom.sun.management.jmxremote.port 远程主机端口号的-Dcom.sun.management.jmxremote ...
- R语言绘制沈阳地铁线路图
##使用leaflet绘制地铁线路图,要求 ##(1)图中绘制地铁线路 library(dplyr) library(leaflet) library(data.table) stations< ...
- LINQ教程二:LINQ操作语法
LINQ查询时有两种语法可供选择:查询表达式语法(Query Expression)和方法语法(Fluent Syntax). 一.查询表达式语法 查询表达式语法是一种更接近SQL语法的查询方式. L ...
- js学习笔记30----对象
面向对象 对象:是一个整体,对外提供一些操作.JavaScript的所有数据都可以被视为对象.简单说,所谓对象,就是一种无序的数据集合,由若干个“键值对”(key-value)构成. 比如说: var ...
- javascript实现记录文本框内文字个数
最近在做一个项目中遇到这样一个问题,要对文本框中用户输入的文字进行记数,在下面显示出来,因为我们做的是一个短信发送平台,现在我们国家的短信服务,如果你的信息超过了70个字符,短信就会按二条给你下发.所 ...