求助58分TLE!!!
查看原帖
求助58分TLE!!!
1339889
pjh0625楼主2025/1/21 20:35
#include <bits/stdc++.h>
using namespace std;

void swapChar(string& a, string& b, int c, int d) {
    char e = a[c - 1];
    a[c - 1] = b[d - 1];
    b[d - 1] = e;
}

bool isValidTrain(const string& f, int g) {
    if (f.size() != g) return false;
    for (char h : f) {
        if (!islower(h)) return false;
    }
    return true;
}

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    if (a < 2 || a > 1000 || b < 1 || b > 100 || c < 0 || c > 100000) {
        return -1;
    }

    string d[1000];
    for (int e = 0; e < a; ++e) {
        cin >> d[e];
        if (!isValidTrain(d[e], b)) {
            return -1;
        }
    }

    int f[100000][4];
    for (int g = 0; g < c; ++g) {
        cin >> f[g][0] >> f[g][1] >> f[g][2] >> f[g][3];
        int h = f[g][0], i = f[g][1], j = f[g][2], k = f[g][3];
        if (h < 1 || h > a || j < 1 || j > a || i < 1 || i > b || k < 1 || k > b || (h == j && i == k)) {
            return -1;
        }
    }

    int l[1000] = {0};
    string m[1000];
    for (int n = 0; n < a; ++n) {
        m[n] = d[n];
    }

    for (int o = 0; o <= c; ++o) {
        unordered_map<string, int> p;
        for (int q = 0; q < a; ++q) {
            p[d[q]]++;
        }

        for (int r = 0; r < a; ++r) {
            l[r] = max(l[r], p[d[r]]);
        }

        if (o < c) {
            int s = f[o][0], t = f[o][1], u = f[o][2], v = f[o][3];
            swapChar(d[s - 1], d[u - 1], t, v);
        }
    }

    for (int w = 0; w < a; ++w) {
        cout << l[w] << endl;
    }

    for (int x = 0; x < a; ++x) {
        d[x] = m[x];
    }

    return 0;
}

2025/1/21 20:35
加载中...