zoukankan      html  css  js  c++  java
  • Equivalent Strings

    Description

    Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are calledequivalent in one of the two cases:

    1. They are equal.
    2. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2, then one of the following is correct:
      1. a1 is equivalent to b1, and a2 is equivalent to b2
      2. a1 is equivalent to b2, and a2 is equivalent to b1

    As a home task, the teacher gave two strings to his students and asked to determine if they are equivalent.

    Gerald has already completed this home task. Now it's your turn!

    Input

    The first two lines of the input contain two strings given by the teacher. Each of them has the length from 1 to 200 000 and consists of lowercase English letters. The strings have the same length.

    Output

    Print "YES" (without the quotes), if these two strings are equivalent, and "NO" (without the quotes) otherwise.

    Sample Input

    Input
    aaba
    abaa
    Output
    YES
    Input
    aabb
    abab
    Output
    NO

    Hint

    In the first sample you should split the first string into strings "aa" and "ba", the second one — into strings "ab" and "aa". "aa" is equivalent to "aa"; "ab" is equivalent to "ba" as "ab" = "a" + "b", "ba" = "b" + "a".

    In the second sample the first string can be splitted into strings "aa" and "bb", that are equivalent only to themselves. That's why string "aabb" is equivalent only to itself and to string "bbaa".

    这道题使用递归枚举,不过要注意剪枝,不然会Memory limit exceeded,原因是堆栈开的太多了。你要一直递归到字符串分为1,那实在是递归太多了.

    可以使用string类做,也可以使用char数组加指针,前者的空间开销和时间开销都要大一些

    方法一  string类:

    #include"iostream"
    using namespace std;
    
    int judge(string a,string b)
    {
         if(a==b) return 1;
         if(a.length()%2) return 0;
         if(a.length()==1&&b.length()==1)
         {
       //      cout<<a[0]<<"    "<<b[0]<<endl;
         if(a[0]==b[0])
         return 1;
         else return 0;
         }
         string a1,a2,b1,b2;
         a1=a.substr(0,a.length()/2);
         a2=a.substr(a.length()/2,a.length());
         b1=b.substr(0,b.length()/2);
         b2=b.substr(b.length()/2,b.length());if(judge(a1,b2)==1&&judge(a2,b1)==1) return 1;
         if(judge(a1,b1)==1&&judge(a2,b2)==1)  return 1;
    
          return 0;
    }
    int main()
    {
        string a,b,a1,a2;
        while(cin>>a>>b)
        {
            if(judge(a,b)) cout<<"YES
    ";
            else cout<<"NO
    ";
        }
        return 0;
    }


    方法二 指针

    #include"iostream"
    #include"cstdio"
    #include"cstring"
    using namespace std;
    
    const int maxn=2700010;
    char a[maxn],b[maxn];
    
    int judge(char* a,char* b,int len)
    {
       if(!strncmp(a,b,len)) return 1;
       if(len%2) return 0;
         
           if((judge(a,b+len/2,len/2)==1&&judge(a+len/2,b,len/2)==1)) {return 1;}
           if((judge(a,b,len/2)==1&&judge(a+len/2,b+len/2,len/2)==1)) return 1;
          return 0;
    }
    int main()
    {
           /* gets(a);
            getchar();
            gets(b);*/
            cin>>a>>b;
            if(judge(a,b,strlen(a))) cout<<"YES
    ";
            else cout<<"NO
    ";
        return 0;
    }
  • 相关阅读:
    格式化输出及基本运算符
    初识python
    项目: python爬虫 福利 煎蛋网妹子图
    mycat重启报错Failed to connect to the Wrapper at port解决方法
    使用nginx加zuul配置
    mycat登录报错Host 'XXX' is blocked because of many connection errors的另一种解决思路
    mycat查表报错Invalid DataSource:0解决方法
    《JUnit实战(第2版)》读书笔记
    博客园的博客积分与排名查看方法
    橄榄球教练不应兼任产品拥有者(译)
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/4684375.html
Copyright © 2011-2022 走看看