Codeforces Round #313 (Div. 2)D

来源:互联网 发布:在淘宝开店怎么取消 编辑:程序博客网 时间:2024/06/07 11:01

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 0x3f3f3f3fusing 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
原创粉丝点击