Mike and strings 题解

发布时间 2023-08-18 22:44:31作者: xvl

题目传送门

一道字符串题。

由于 \(n\) 非常小,可以暴力枚举字符串。我们可以枚举其中一个字符串 \(s_i\),然后让其他的字符串变成 \(s_i\),最后记录一下次数,取一个最小值即可。

在枚举第二个字符串的时候可以将它再复制一份自己到后面,然后可以用 find 函数来统计。当然,如果找不到,这个字符串永远不可能变成 \(s_i\),输出 \(-1\)

Code

#include <bits/stdc++.h>

namespace xvl_ { 
    #define ll long long
    #define IMAX INT_MAX;
    #define LMAX LONG_LONG_MAX
    void debug() { std :: cerr << "debug" << "\n"; } 
    template <typename T> inline T Max(T a, T b) { return a > b ? a : b; }
    template <typename T> inline T Min(T a, T b) { return a < b ? a : b; }
    template <typename T, typename... Args> inline T Max(T a, Args... args) { return a > Max(args...) ? a : Max(args...); }
    template <typename T, typename... Args> inline T Min(T a, Args... args) { return a < Min(args...) ? a : Min(args...); }
}
using namespace std;
using namespace xvl_;
int n, ans = IMAX;
string s[55];
int main() {
    /*
    freopen("InName.in", "r", stdin);
    freopen("OutName.out", "w", stdout);
    */
    ios :: sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> s[i];
    for (int i = 1; i <= n; i++) {
        int cnt = 0;
        for (int j = 1; j <= n; j++) {
            if ((s[j] + s[j]).find(s[i]) != -1) cnt += (s[j] + s[j]).find(s[i]); // 加上次数
            else {
                cout << -1; // 无解
                return 0;
            }
        }
        ans = Min(ans, cnt); 
    }
    cout << ans;
    return 0;
}