cf559B字符串的最小表示法和递归的写法

这题有俩种做法,一种递归,一种字符串的最小表示法,刚开始自己写了一发递归,结果一个递归里面有俩个return 0,呵呵了。

自己连正确的递归姿势都不会,其实可以把俩个return放到俩个函数里面。

递归法:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
using namespace std;
#define LL long long
const int N=200000+10;
char A[N],B[N];
bool cmp(char x[],char y[],int len)
{
    bool isok=1;
    for(int i=0;i<len;i++)
    {
        if(x[i]!=y[i])
            isok=0;
    }
    return isok;
}
bool equ(char x[],char y[],int len)
{
    if(cmp(x,y,len)) return 1;
    if(len%2==0)
    {
        if(equ(x,y+len/2,len/2))
            return equ(x+len/2,y,len/2);
        else
            return equ(x+len/2,y+len/2,len/2)&&equ(x,y,len/2);
    }
    return 0;
}
int main()
{
    scanf("%s%s",A,B);
    int len=strlen(A);
    printf("%s\n",equ(A,B,len)?"YES":"NO");
    return 0;
}

字符串的最小表示法:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<set>
#include<map>
#include<string>
using namespace std;
const int N=200005;
string a,b;
string smallest(string s)
{
    if(s.length()%2==1) return s;
    string s1=smallest(s.substr(0,s.length()/2));
    string s2=smallest(s.substr(s.length()/2,s.length()/2));
    if(s1<s2) return s1+s2;
    else
        return s2+s1;
}
int main()
{
    cin>>a;
    cin>>b;
    a=smallest(a);
    b=smallest(b);
    printf("%s\n",a==b?"YES":"NO");
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值