UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#212188#3816. 元素PanjunnanCompile Error//C++111.4kb2024-10-13 11:56:112024-10-13 12:31:17

answer

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

int findMinTime(const string& S, const string& T) {
    int n = S.length();
    int m = T.length();
    unordered_map<char, vector<int>> charPositions;
    for (int i = 0; i < n; ++i) {
        charPositions[S[i]].push_back(i);
    }
    int currentPos = -1;
    int totalTime = 0;
    for (int i = 0; i < m; ++i) {
        char targetChar = T[i];
        if (charPositions.find(targetChar) == charPositions.end()) {
            return -1;
        }
        vector<int>& positions = charPositions[targetChar];
        int minTime = INT_MAX;
        for (int pos : positions) {
            int time = abs(pos - currentPos);
            if (currentPos == -1) {
                time = 0;
            }
            minTime = min(minTime, time);
        }
        if (minTime == INT_MAX) {
            return -1;
        }
        totalTime += minTime;
        currentPos = positions[0];
        for (int j = 1; j < positions.size(); ++j) {
            if (abs(positions[j] - currentPos) < abs(positions[currentPos] - currentPos)) {
                currentPos = positions[j];
            }
        }
    }
    return totalTime;
}

int main() {
    int n, m;
    cin >> n >> m;
    string S, T;
    cin >> S >> T;
    int result = findMinTime(S, T);
    cout << result << endl;
    return 0;
}

详细

answer.code: In function 'int findMinTime(const string&, const string&)':
answer.code:23:23: error: 'INT_MAX' was not declared in this scope
         int minTime = INT_MAX;\x0d
                       ^