[C ++] recursive string equal weak

substr function

Two usage:

string str.substr (nStart) // default string str nStart position from the start until the end, taken str

string str.substr (nStart, nLength) // string str from the start position nStart interception nLength characters! If nLength> remaining characters are taken up to the end str

For example:

string str("12345asdf");

string strTmp1 = str.substr (1); // get the string str starting from bit 1 to the end of the string, strTmp1 values: "2345asdf"
String strTmp2 str.substr = (l, 5); // get the length of the string s starting from the first bit string of 5, strTmp1 values: "2345a"
 

Weak string is equal to

If the two strings a, b satisfy one of the following two conditions, we call a weak equal to b, b or weakly equal a.
1.a equal to b;
2.a, b of the same length, and have an even length, apart from the string a string in the middle, split into a1, a2, b to the same string is split into b1, b2. b1 a1 and a2 is equal to weak weak equal to b2, a1 or a2 and b2 is equal to weak weak equal to b1.
Input format
input a total of two rows of a character string, string contains only lowercase letters.
Output Format
If the input string is weak equal to two, output "YES", otherwise outputs NO. ,

 

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n,a,b,p;
bool requal(string s1,string s2){
    if(s1 == s2)
        return true;
    else if(s1.length() > 1){
        string s11 = s1.substr(0,s1.length()/2);
        string s12 = s1.substr(s1.length()/2);
        string s21 = s2.substr(0,s2.length()/2);
        string s22 = s2.substr(s2.length()/2);
        return (requal(s11,s21) && requal(s12,s22)) || (requal(s12,s21) && requal(s11,s22));
        
    }
    return false;
}
int main(){
    string t1,t2;
    cin >> t1 >> t2;
    int flag = 0,l = t1.length(),k = 1;
    while(l > k){
        k *= 2;
    }
    if(k != l){
    	flag = 0;
    }
    if(t1.length()!=t2.length()){
        printf("NO");
    }
    else if(flag){
        printf("NO");
    }
    else if(requal(t1,t2)){
        printf("YES");
    }
    else {
        printf("NO");
    }
    return 0;
}


 

Published 38 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41514794/article/details/104682812