UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#211988#3817. 写字PanjunnanCompile Error//C++2.6kb2024-10-13 09:38:402024-10-13 12:09:53

answer

#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;
struct State {
    int index;   
    int time;    
    int tIndex; 
};

int minTimeToWrite(const string& S, const string& T) {
    int lenS = S.size();
    int lenT = T.size();
    unordered_map<char, vector<int>> charPositions;
    for (int i = 0; i < lenS; ++i) {
        charPositions[S[i]].push_back(i);
    }
    queue<State> q;
    vector<vector<bool>> visited(lenS, vector<bool>(lenT + 1, false));
    for (int i = 0; i < lenS; ++i) {
        if (S[i] == T[0]) {
            q.push({i, 0, 1}); 
            visited[i][1] = true; 
        }
    }

    while (!q.empty()) {
        State current = q.front();
        q.pop();


        if (current.tIndex == lenT) {
            return current.time;
        }

        char currentChar = S[current.index];

        if (current.index > 0 && !visited[current.index - 1][current.tIndex]) { // 左
            if (S[current.index - 1] == T[current.tIndex]) {
                visited[current.index - 1][current.tIndex] = true;
                q.push({current.index - 1, current.time + 1, current.tIndex + 1});
            } else {
                visited[current.index - 1][current.tIndex] = true;
                q.push({current.index - 1, current.time + 1, current.tIndex});
            }
        }
        if (current.index < lenS - 1 && !visited[current.index + 1][current.tIndex]) { // 右
            if (S[current.index + 1] == T[current.tIndex]) {
                visited[current.index + 1][current.tIndex] = true;
                q.push({current.index + 1, current.time + 1, current.tIndex + 1});
            } else {
                visited[current.index + 1][current.tIndex] = true;
                q.push({current.index + 1, current.time + 1, current.tIndex});
            }
        }
        for (int pos : charPositions[currentChar]) {
            if (pos != current.index && !visited[pos][current.tIndex]) {
                if (pos < current.index) {
                    visited[pos][current.tIndex] = true;
                    q.push({pos, current.time + (current.index - pos), current.tIndex});
                } else {
                    visited[pos][current.tIndex] = true;
                    q.push({pos, current.time + (pos - current.index), current.tIndex});
                }
            }
        }
    }
    return -1;
}

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

详细

In file included from /usr/include/c++/4.8/unordered_map:35:0,
                 from answer.code:3:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
answer.code: In function 'int minTimeToWrite(const string&, const string&)'...