• CodeForces


    D. Equivalent Strings
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent 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.

    Examples
    input
    aaba
    abaa
    output
    YES
    input
    aabb
    abab
    output
    NO
    Note

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

    题目大意:判断两个字符串是不是“equivalent”。如果两个字符串相等,则是。将字符串分别从中间分成a1/a2两部分b1/b2两部分,若a1/b1,a2/b2是equivalent,或a1/b2,a2/b1是equivalent ,则s1/s2也是equivalent 。

    解题思路:暴力,直接用dfs,设置结束条件:①两个字符串相等-----成功②若不相等且长度为奇数-----失败③若长度为2,直接判断字符是否相等。④将每个字符串分解成两个子串继续dfs。

    注意:开始本来建了好多字符数组。。发现不方便操作,直接用下标更加方便。代码思路很清晰,应该挺好懂的。。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=2e5+10;
    char a1[maxn],a2[maxn];
    
    bool check(int l1,int r1,int l2,int r2)
    {
        int i,j;
        for(i=l1,j=l2;i<=r1;i++,j++)
        {
            if(a1[i]!=a2[j])
                return false;
        }
        return true;
    }
    
    bool dfs(int l1,int r1,int l2,int r2)
    {
        int L = r1-l1+1;
        //printf("dfs-%d %d %d %d L--%d
    ",l1,r1,l2,r2,L);
        if(check(l1,r1,l2,r2))
            return true;
        if(L==2)
        {
            //printf("L==2tepan
    ");
            if((a1[l1]==a2[l2]&&a1[r1]==a2[r2])||(a1[r1]==a2[l2]&&a1[l1]==a2[r2]))
                return true;
            else
                return false;
        }
        if(L%2!=0)
            return false;
        int t=L/2;
        if( ( dfs(l1,l1+t-1,l2,l2+t-1)&&dfs(l1+t,r1,l2+t,r2) ) )
        {
            return true;
        }
        if( ( dfs(l1,l1+t-1,l2+t,r2)&&dfs(l1+t,r1,l2,l2+t-1) ) )
            return true;
        return false;
    }
    
    int main()
    {
        scanf("%s",a1);
        scanf("%s",a2);
        int l = strlen(a1);
        if(dfs(0,l-1,0,l-1))
        {
            printf("YES
    ");
        }
        else
            printf("NO
    ");
    }
  • 相关阅读:
    经典假设检验理论记录一二
    阿里云centos7.3安装tomcat8
    PowerDesigner中Name与Code同步的问题
    PowerDesigner中NAME和COMMENT的互相转换
    树形结构的数据库表设计
    Spring mybatis Access denied for user 'root'@'localhost' (using password:YES)
    IOS上架审核问题
    maven自动部署war包到tomcat 问题
    SpringMVC +Hibernate JPA+Spring-data-jpa
    Hibernate4.3.x Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
  • 原文地址:https://www.cnblogs.com/WWkkk/p/7418259.html
Copyright © 2020-2023  润新知