Equivalent Strings

Equivalent Strings

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/E

题意:

   给出两个字符串,确定是否相等。一个字符串分割成相同大小的两半a1和a2,另一个字符串分割成相同大小的成两半b1和b2。

以下是正确的:
a1相当于b1 ,a2相当于b2
a1相当于b2 ,a2相当于b1

 

Sample Input

Input
aaba
abaa
Output
YES
Input
aabb
abab
Output
NO

Hint

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

分析:

求出字符串的长度l。

如果为奇数串,只能比较是否每个字符相同,

如果为偶数串,第一个串分成两个相等长度的串为a, a+l/2,

第二个串也分成b, b+l/2,判断a== b && a+l/2 == b+l/2 || a == b+l/1 && a+l/2 == b.

 
  
#include<iostream>
#include<cstring>
using namespace std; const int maxn=200001; char a[maxn],b[maxn]; int bj(char*a,char*b,int l) //比较两个字符串是否相等 { for(int i=0;i<l;i++) { if(a[i]!=b[i]) return 0; } return 1; } int Do(char*a,char*b,int l) { int f=0; if(bj(a,b,l)==1) f=1; if(l%2==1) // 奇字符串 { if(f==1)return 1; else return 0; } else{ //偶字符串 if(f==1) return 1; else { l=l/2; if(Do(a,b,l)&&Do(a+l,b+l,l)||Do(a,b+l,l)&&Do(a+l,b,l)) //判断 return 1; else return 0; } } } int main() { cin>>a>>b; int x=strlen(a); if(Do(a,b,x)==1) cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; }
 
 

 

转载于:https://www.cnblogs.com/fenhong/p/4684333.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值