问一道经典题
查看原帖
问一道经典题
745763
Diamond_Sword_kent楼主2024/10/5 14:45

题目链接
我的代码:

#include <bits/stdc++.h>
using namespace std;
string s;
int n, a, b;
string res[15];
bool cmp(string s, int a1, int b1) {
    while (1) {
        if (s[a1] != s[b1]) {
            if (s[a1] < s[b1])
                return 0;
            return 1;
        }
        a1++, b1++;
        if (a1 > n)
            a1 -= n;
        if (b1 > n)
            b1 -= n;
    }
}
bool cmp_zdx(int a1, int b1, string res[]) {
    for (int i = 0; i < n; i++) {
        if (res[a1][i] != res[b1][i]) {
            if (res[a1][i] < res[b1][i])
                return 0;
            return 1;
        }
    }
    return 0;
}
string d1(string s) {
    string s1 = s;
    for (int i = 0; i < 10; i++) {
        int mn = 1;
        for (int j = 3; j <= n; j += 2) {
            if (cmp(s1, mn, j))
                mn = j;
        }
        res[i] = "";
        for (int j = mn; j <= n; j++) res[i] += s1[j];
        for (int j = 1; j < mn; j++) res[i] += s1[j];
        for (int j = 2; j <= n; j += 2) {
            s1[j] = (char)((s1[j] - '0' + a) % 10 + '0');
        }
    }
    int mn = 0;
    for (int i = 1; i < 10; i++) {
        if (cmp_zdx(mn, i, res))
            mn = i;
    }
    return res[mn];
}
int main() {
    //	freopen("operate.in","r",stdin);
    //	freopen("operate.out","w",stdout);
    cin >> s;
    n = s.length();
    s = ' ' + s;
    scanf("%d%d", &a, &b);
    if (b % 2) {
        string res_[15], s2 = s;
        for (int i = 0; i < 10; i++) {
            res_[i] = d1(s2);
            for (int j = 1; j <= n; j += 2) {
                s2[j] = (char)((s2[j] - '0' + a) % 10 + '0');
            }
        }
        int mn = 0;
        for (int i = 1; i < 10; i++) {
            if (cmp_zdx(mn, i, res_))
                mn = i;
        }
        cout << res_[mn];
    } else {
        cout << d1(s);
    }
    return 0;
}

“|”号的处理似乎有些问题,望大佬指正

2024/10/5 14:45
加载中...