Codeforces Round #313 (Div. 2)D

D. Equivalent Strings

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

题目定义一种新的字符串的相等, 就是如果现在两个字符串不等的话, 就把这个两个平均分为两个字符串(要偶数才能分,奇数长度的字符串如果使不等的话就直接不等了.), 然后前半段和后半段可以随意搭配(就是两种情况), 然后再按上面的方法来判定是否相等.

有种暴力可以水过的方法就是一直递归下去,然后判断,不等继续分直到长度是奇数.等的话就返回1了.

但是偶然之间看到别人的代码写的是类似于归并排序一般处理(其实是只递归,不合并),然后看最后两者是否相等.

题目中的方法就是不断分治的,而且没有前后顺序关系的.所以这么做确实很有道理….只能怪自己没有很好的分析到…..还是太蠢了…

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<iostream>
#define pb push_back
#define INF 0x3f3f3f3f
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;
const int mod = 1000000007;
const int N = 200005;
char str1[N], str2[N];
int check(int l1, int r1, int l2, int r2)
{
    int i, j;
    for(i=l1, j=l2; i<r1; i++,j++) if(str1[i] != str2[j])
        break;
    if(i>=r1) return 1;
    if((r1 - l1)%2 == 1 || (r2-l2)%2 == 1) return 0;
    int mid1 = (l1 + r1)>>1, mid2 = (l2 + r2)>>1;
    return ((check(l1, mid1, l2, mid2) && check(mid1, r1, mid2, r2)) || (check(l1, mid1, mid2, r2) && check(mid1, r1, l2, mid2)));
}
void solve()
{
    scanf("%s %s", str1, str2);
    int len = strlen(str1);
    puts(check(0, len, 0, len)?"YES":"NO");
}
int main(void)
{
#ifdef DK1
    freopen("/home/dk/桌面/1.in","r",stdin);
#endif // DK
    solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值