• 3thweek——E.暴力求解+DFS


    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".

    分析步骤:

    1.先判断两串字符串长度为奇数还是偶数,奇数则要遍历全部的两个字符串,字符和顺序都相同才能输出YES,否则输出NO。

    2.如果是偶数,两个字符串完全相同就直接输出YES,否则将两个字符串各切成两个子字符串再比较。

    代码如下:

     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 const int maxn=200005;
     5 char a[maxn],b[maxn];
     6 
     7 int i,flag=0;
     8 int compare(char* a1,char* b1,int l)  //自定义比较函数
     9 {   
    10     for(i=0;i<l;i++)
    11     {
    12         if(a1[i]!=b1[i])
    13         {
    14             return 0;
    15         }
    16     }
    17     flag=1;
    18     return flag;
    19 }
    20     
    21 
    22 int fun(char* a1,char* b1,int l)
    23 {   
    24     int k=0;
    25     if(compare(a1,b1,l))
    26            k=1; 
    27     if(l%2==1)
    28     {
    29         if(k==1)
    30            return 1;
    31         else
    32             return 0;
    33     }    
    34     else
    35     {
    36         if(k==1)
    37             return 1;
    38     else
    39     {
    40             l=l/2;
    41           if( (fun(a1,b1,l)  && fun(a1+l,b1+l,l)) || (fun(a1,b1+l,l) && fun(a1+l,b1,l)) )   
    42             return 1;
    43           else
    44               return 0;
    45     }
    46     }
    47 }
    48 
    49 int main()
    50 {
    51     cin>>a;
    52     cin>>b;
    53     int len=strlen(a);
    54     if(fun(a,b,len))
    55        cout<<"YES"<<endl;
    56     else
    57         cout<<"NO"<<endl;
    58 return 0;
    59 }
     
  • 相关阅读:
    退背包
    杜教筛BM
    Petya and Array CodeForces
    AC自动机模板
    KMP模板
    Docker系列器九:docker-compose与docker-compose.yml语法
    Fabric的简单Web应用
    ubuntu防火墙
    ubuntu16.04 HyperLedger Fabric 1.2.0 开发环境搭建
    crypto-config.yaml
  • 原文地址:https://www.cnblogs.com/x512149882/p/4695928.html
Copyright © 2020-2023  润新知