首页 > 其他 > 详细

Codeforces Round #313 (Div. 2) D.Equivalent Strings (字符串)

时间:2016-01-19 15:39:19      阅读:242      评论:0      收藏:0      [点我收藏+]

感觉题意不太好懂 = =#

给两个字符串 问是否等价
等价的定义(满足其中一个条件):1.两个字符串相等 2.字符串均分成两个子串,子串分别等价

因为超时加了ok函数剪枝,93ms过的。

#include <iostream>
#include <cstring>
#define clr(x,c) memset(x,c,sizeof(x))
using namespace std;

const int N = 200005;
char s[N], t[N];
int sc[30], tc[30];

bool ok(char s[], char t[], int len)
{
    clr(sc, 0); clr(tc, 0);
    for (int i = 0; i < len; ++i) {
        sc[ s[i] - ‘a‘ ]++;
        tc[ t[i] - ‘a‘ ]++;
    }
    for (int i = 0; i < 26; ++i) {
        if (sc[i] != tc[i]) return false;
    }
    return true;
}

bool equ(char s[], char t[], int len)
{
    if (!strncmp(s, t, len)) return true;
    if (len % 2) return false;

    if (!ok(s, t, len)) return false;

    int l = len / 2;
    if (equ(s, t, l) && equ(s + l, t + l, l)) return true;
    if (equ(s, t + l, l) && equ(s + l, t, l)) return true;
    return false;
}

int main()
{
    std::ios::sync_with_stdio(false);
    while (cin >> s >> t) {
        if (equ(s, t, strlen(s))) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}

  

Codeforces Round #313 (Div. 2) D.Equivalent Strings (字符串)

原文:http://www.cnblogs.com/wenruo/p/5142264.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!