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

时间:2023-03-09 01:49:35
Codeforces Round #313 (Div. 2) D.Equivalent Strings (字符串)

感觉题意不太好懂 = =#

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