您的位置:首页 > 产品设计 > UI/UE

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

2016-01-19 14:58 603 查看
感觉题意不太好懂 = =#

给两个字符串 问是否等价
等价的定义(满足其中一个条件):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
, t
;
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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: