AcWing 无. 传匹配
原题链接
中等
作者:
Florentino
,
2021-05-16 15:02:31
,
所有人可见
,
阅读 247
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <cmath>
#include <time.h>
#include <sys/timeb.h>
#include <algorithm>
#include <set>
#include <limits.h>
#define x first
#define y second
using namespace std;
string s[4];
bool match[4][5010];
int main()
{
for (int i = 1; i <=3; i++) {
cin >> s[i];
}
int lenth = s[1].length();
for (int i = 0; i < lenth; i++) {
for (int j = 0; j < lenth; j++) {
if (s[1][(i + j) % lenth] == s[2][j]) {
match[1][i] = 1;
}
if (s[1][(i + j) % lenth] == s[3][j]) {
match[2][i] = 1;
}
if (s[2][(i + j) % lenth] == s[3][j]) {
match[3][i] = 1;
}
}
}
/*for (int i = 1; i <= 3; i++) {
for (int j = 0; j < lenth; j++) {
cout << match[i][j];
}
cout << endl;
}*/
int res = INT_MAX;
for (int i = 0; i < lenth; i++) {
for (int j = 0; j < lenth; j++) {
if (match[1][(i-j+ lenth)%lenth]==0 && match[2][i] == 0 && match[3][j] == 0) {
res = min(res, max(i, j));
res = min(res, max(lenth - i, lenth - j));
res = min(res, (min(i, lenth - i) + min(j, lenth - j)));
}
}
}
if (res == INT_MAX)cout << "-1";
else cout << res;
return 0;
}