cf559B字符串的最小表示法和递归的写法
2015-07-23 21:12
288 查看
这题有俩种做法,一种递归,一种字符串的最小表示法,刚开始自己写了一发递归,结果一个递归里面有俩个return 0,呵呵了。
自己连正确的递归姿势都不会,其实可以把俩个return放到俩个函数里面。
递归法:
字符串的最小表示法:
自己连正确的递归姿势都不会,其实可以把俩个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; }
相关文章推荐
- 解决java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind
- hdoj 1106 排序
- 205 Isomorphic Strings
- undefined reference to `log4cplus::Logger::Logger(log4cplus::Logger&&)'
- iOS 监听文本框的改变 代码片段
- 7.23多校——5305DFS——Friends
- Parencodings
- SQL*loader实验
- C语言部分知识
- Android之——监听手机开机事件
- hdu 3635 Dragon Balls
- Hdu 2899 - Strange fuction 二分/三分求函数极值点
- 享元模式
- hdu5305
- CUDA快速傅里叶变换(cuFFT)阅读笔记(一)
- 03 JAVA 数组补充 拷贝、序列化
- After Effect手写字效果
- hashMap实现原理
- Linux下安装Eclipse没有Eclipse Marketplace 选项
- SQL*loader实验