D. Equivalent Strings
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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:

  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 test(s)
input
aaba
abaa
output
YES
input
aabb
abab
output
NO
Note

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".

题目链接:点击打开链接

定义两字符串相等的2种情况, 1是各个字母相应同样, 2是a字符串分为两半a1, a2, b字符串分为两半b1, b2, 当且仅当(a1 == b1 && a2

== b2) || (a1 == b2 && a2 == b1), 推断两字符串是否相等.

深搜, 每一次dfs先推断s1, s2的len个字母是否相等, 若len为奇数直接return false, 进而继续分两种情况进行dfs.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 5;
char s1[MAXN], s2[MAXN];
bool dfs(char s1[], char s2[], int len)
{
if(!strncmp(s1, s2, len)) return true;
if(len & 1) return false;
int x = len >> 1;
if(dfs(s1, s2 + x, x) && dfs(s1 + x, s2, x)) return true;
if(dfs(s1, s2, x) && dfs(s1 + x, s2 + x, x)) return true;
return false;
}
int main(int argc, char const *argv[])
{
scanf("%s%s", s1, s2);
if(dfs(s1, s2, strlen(s1))) printf("YES\n");
else printf("NO\n");
return 0;
}

Codeforces Round #313 (Div. 2) 560D Equivalent Strings(dos)的更多相关文章

  1. Codeforces Round #313 (Div. 1) B. Equivalent Strings

    Equivalent Strings Problem's Link: http://codeforces.com/contest/559/problem/B Mean: 给定两个等长串s1,s2,判断 ...

  2. Codeforces Round #313 (Div. 2) D. Equivalent Strings

    D. Equivalent Strings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559/ ...

  3. Codeforces Round #313 (Div. 1) B. Equivalent Strings DFS暴力

    B. Equivalent Strings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559 ...

  4. Codeforces Round #313 (Div. 2) D.Equivalent Strings (字符串)

    感觉题意不太好懂 = =# 给两个字符串 问是否等价等价的定义(满足其中一个条件):1.两个字符串相等 2.字符串均分成两个子串,子串分别等价 因为超时加了ok函数剪枝,93ms过的. #includ ...

  5. Codeforces Round #313 (Div. 2)(A,B,C,D)

    A题: 题目地址:Currency System in Geraldion 题意:给出n中货币的面值(每种货币有无数张),要求不能表示出的货币的最小值.若全部面值的都能表示,输出-1. 思路:水题,就 ...

  6. Codeforces Round #313 (Div. 2) 解题报告

    A. Currency System in Geraldion: 题意:有n中不同面额的纸币,问用这些纸币所不能加和到的值的最小值. 思路:显然假设这些纸币的最小钱为1的话,它就能够组成随意面额. 假 ...

  7. codeforces 559b//Equivalent Strings// Codeforces Round #313(Div. 1)

    题意:定义了字符串的相等,问两串是否相等. 卡了时间,空间,不能新建字符串,否则会卡. #pragma comment(linker,"/STACK:1024000000,102400000 ...

  8. Codeforces Round #313 (Div. 2) A.B,C,D,E Currency System in Geraldion Gerald is into Art Gerald's Hexagon Equivalent Strings

    A题,超级大水题,根据有没有1输出-1和1就行了.我沙茶,把%d写成了%n. B题,也水,两个矩形的长和宽分别加一下,剩下的两个取大的那个,看看是否框得下. C题,其实也很简单,题目保证了小三角形是正 ...

  9. Codeforces Round #302 (Div. 1) C. Remembering Strings DP

    C. Remembering Strings Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

随机推荐

  1. PCB 无需解压,直接读取Zip压缩包指定文件 实现方法

    最近有一项需求,将电测试点数后台批量写入到工程系统流程指示中,而电测试文件存在压缩包中,压缩包存在公共网络盘 示例图: 一.采用原始方法(4步完成): 第1步:.网络盘ZIP拷到本地, 第2步:解压Z ...

  2. MySQL-基础操作之增删改查

    1.增 (1)创建数据库dks create database dks; (2)创建名为t1的表,并指定引擎和字符集: ) not null,ages int) engine=innodb defau ...

  3. P1401 城市(30分,正解网络流)

    题目描述 N(2<=n<=200)个城市,M(1<=m<=40000)条无向边,你要找T(1<=T<=200)条从城市1到城市N的路,使得最长的边的长度最小,边不能 ...

  4. CSS清除浮动_清除float浮——详解overflow:hidden 与clear:both属性

    最近刚好碰到这个问题,看完这个就明白了.写的很好,所以转载了! CSS清除浮动_清除float浮动 CSS清除浮动方法集合 一.浮动产生原因   -   TOP 一般浮动是什么情况呢?一般是一个盒子里 ...

  5. web前端利用HTML代码显示符号

    HTML常用符号代码:                       ´ ´ © © > > µ µ ® ® & & ° ° ¡ ¡     » » ¦ ¦ ÷ ÷ ¿ ¿ ...

  6. Spring 错误日志

    13:19:14.437 [main] DEBUG org.springframework.boot.context.logging.ClasspathLoggingApplicationListen ...

  7. 【PostgreSQL-9.6.3】表操作语句

    1.创建数据表 create table table_name ( 字段1 数据类型[列级别约束条件][默认值], 字段2 数据类型[列级别约束条件][默认值], 字段3 数据类型[列级别约束条件][ ...

  8. 【SQL】通过rowid查找及删除重复记录

    新建T表如下: SQL> select * from t; X Y ---------- --          1 a          1 a          1 a          2 ...

  9. 配置 Sybase数据源

    1.Start-- > All Programs -- > Sybase - -> Connectivity --> click ‘Open Client Directory ...

  10. 安卓使用JNI-NDK

    详细配置,参考链接:http://www.jb51.net/softjc/115204.html 一  .为什么使用NDK 1.代码的保护.由于apk的java层代码很容易被反编译,而C/C++库反汇 ...