UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#212182#3817. 写字PanjunnanCompile Error//C++112.1kb2024-10-13 11:53:382024-10-13 12:30:36

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;
}

详细

answer.code: In function 'int main()':
answer.code:17:5: error: 'map' was not declared in this scope
     map<char, vector<int>> positions;\x0d
     ^
answer.code:17:9: error: expected primary-expression before 'char'
     map<char, vector<int>> positions;\x0d
         ^
answer.code:17:9: error: expected ';' before 'char'
answer.code:19:9: error: 'positions' was not declared in this scope
         positions[S[i]].push_back(i);\x0d
         ^
answer.code:27:13: error: 'positions' was not declared in this ...