UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212152#3817. 写字yangtianming001Compile Error//C++111.8kb2024-10-13 11:38:022024-10-13 12:27:37

answer

#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <climits>
#include <tuple>

using namespace std;

int find_min_time(string S, string T) {
    int n = S.length(), m = T.length();
    
    unordered_map<char, vector<int>> char_pos;
    for (int i = 0; i < n; i++) {
        char_pos[S[i]].push_back(i);
    }

    for (char c : T) {
        if (char_pos.find(c) == char_pos.end()) {
            return -1;
        }
    }

    vector<vector<int>> dp(n, vector<int>(m, INT_MAX));
    
    queue<tuple<int, int, int>> q;

    for (int start_pos : char_pos[T[0]]) {
        q.push(make_tuple(start_pos, 0, 1));
        dp[start_pos][0] = 1;
    }

    while (!q.empty()) {
        auto [s_pos, t_pos, time] = q.front();
        q.pop();

        if (t_pos == m - 1) {
            return time;
        }

        char next_char = T[t_pos + 1];

        if (s_pos - 1 >= 0 && dp[s_pos - 1][t_pos] > time + 1) {
            dp[s_pos - 1][t_pos] = time + 1;
            q.push(make_tuple(s_pos - 1, t_pos, time + 1));
        }
        if (s_pos + 1 < n && dp[s_pos + 1][t_pos] > time + 1) {
            dp[s_pos + 1][t_pos] = time + 1;
            q.push(make_tuple(s_pos + 1, t_pos, time + 1));
        }

        for (int jump_pos : char_pos[next_char]) {
            if (jump_pos != s_pos && dp[jump_pos][t_pos + 1] > time + abs(jump_pos - s_pos)) {
                dp[jump_pos][t_pos + 1] = time + abs(jump_pos - s_pos);
                q.push(make_tuple(jump_pos, t_pos + 1, dp[jump_pos][t_pos + 1]));
            }
        }
    }

    return -1;
}

int main() {
    int n, m;
    string S, T;

    cin >> n >> m;
    cin >> S;
    cin >> T;

    cout << find_min_time(S, T) << endl;

    return 0;
}

Details

answer.code: In function 'int find_min_time(std::string, std::string)':
answer.code:34:14: error: expected unqualified-id before '[' token
         auto [s_pos, t_pos, time] = q.front();\x0d
              ^
answer.code:37:13: error: 't_pos' was not declared in this scope
         if (t_pos == m - 1) {\x0d
             ^
answer.code:38:20: error: invalid conversion from 'time_t (*)(time_t*)throw () {aka long int (*)(long int*)throw ()}' to 'int' [-fpermissive]
             return time;\x0d
               ...