UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212150#3817. 写字PanjunnanCompile Error//C++2.1kb2024-10-13 11:36:492024-10-13 12:27:28

answer

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <climits>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    
    string S, T;
    cin >> S >> T;
    
    // 记录 S 中每个字符的索引
    map<char, vector<int>> positions;
    for (int i = 0; i < n; i++) {
        positions[S[i]].push_back(i);
    }
    
    // 当前位置的最小时间
    int total_time = 0;
    int current_pos = -1; // 初始化为无效位置

    for (char c : T) {
        if (positions.find(c) == positions.end()) {
            // 如果 T 中有 S 不存在的字符
            cout << -1 << endl;
            return 0;
        }

        // 找到 S 中当前字符 c 的位置
        vector<int> &indices = positions[c];
        if (current_pos == -1) {
            // 如果当前没有写任何字符, 从 S 的第一个位置开始
            total_time += indices[0] + 1; // 从 S[0] 写到 S[indices[0]] 需要的时间
            current_pos = indices[0];
        } else {
       
            auto it = lower_bound(indices.begin(), indices.end(), current_pos);
            int next_pos;

            if (it == indices.end()) {

                next_pos = indices.front();
                total_time += abs(current_pos - next_pos) + 1; 
            } else if (it == indices.begin()) {

                next_pos = indices[0];
                total_time += abs(current_pos - next_pos) + 1;
            } else {

                int left_pos = *(--it); 
                int right_pos = indices.front();
                int left_time = abs(current_pos - left_pos) + 1;
                int right_time = abs(current_pos - right_pos) + 1;


                if (left_time <= right_time) {
                    next_pos = left_pos;
                    total_time += left_time;
                } else {
                    next_pos = right_pos;
                    total_time += right_time;
                }
            }
            current_pos = next_pos;
        }
    }

    cout << total_time << endl;
    return 0;
}

Details

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 main()':
answer.code:17:5: error: 'map' was n...