Codeforces 559B - Equivalent Strings

559B - Equivalent Strings

思路:字符串处理,分治

不要用substr(),会超时

AC代码:

#include<bits/stdc++.h>
#include<cstring>
using namespace std;
#define ll long long
const int N=2e5+5;
bool cmp(char a[],char b[],int l)
{
    bool ans=true;
    for(int i=0;i<l;i++)if(a[i]!=b[i])ans=false;
    return ans;
}
bool eq(char a[],char b[],int l)
{
    if(cmp(a,b,l))return true;
    if(l&1)return false;
    if(eq(a,b+l/2,l/2)&&eq(a+l/2,b,l/2)||eq(a,b,l/2)&&eq(a+l/2,b+l/2,l/2))return true;
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    char s1[N],s2[N];
    cin>>s1>>s2;
    if(eq(s1,s2,strlen(s1)))cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
} 

TLE代码:

#include<bits/stdc++.h>
#include<cstring>
using namespace std;
#define ll long long
bool eq(string a,string b)
{
    if(a==b)return true;
    if(a.size()!=b.size())return false;
    if(a.size()&1)return false;
    string a1=a.substr(0,a.size()/2),a2=a.substr(a.size()/2,a.size()/2);
    string b1=b.substr(0,b.size()/2),b2=b.substr(b.size()/2,b.size()/2);
    if((eq(a1,b1)&&eq(a2,b2))||(eq(a1,b2)&&eq(a2,b1)))return true;
    else return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s1,s2;
    cin>>s1>>s2;
    if(eq(s1,s2))cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
} 

 

转载于:https://www.cnblogs.com/widsom/p/7201469.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值