您的位置:首页 > 其它

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

2015-07-23 21:12 288 查看
这题有俩种做法,一种递归,一种字符串的最小表示法,刚开始自己写了一发递归,结果一个递归里面有俩个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
,B
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: