a536中等記錄每個時間欄的繪製高度

CPE:Not That Kind of Graph (UVa 10800)

先模擬高度,再由高至低輸出保留空間的圖形。

前往題目練習 →回報教學錯誤每筆時間 O(L²),空間 O(L)(逐列輸出)。

解題思路

每個指令對應一個欄位。R 在目前高度畫 / 後升高;F 先降一格再畫反斜線;C 在目前高度畫 _。保存每欄實際畫字元的高度,並追蹤最低與最高繪製列,而不是直接拿走勢頂點當畫布範圍。

輸出從最高繪製列到最低列,每列先放 | 與一個空白,再依欄位放圖字元或空白,最後刪除行尾空白。最低圖列下方立刻畫 + 與 len+2 個減號。每組先印 Case #k:,圖後再空一行;負高度只影響列的相對位置。

Step-by-step animation

示意:指令 RCF

回報動畫錯誤
步驟 1 / 3

R

在高度 0 畫 /,之後高度變 1。

欄0:/@0
目前高度
1
最低繪製列
0

Reference solution

回報程式錯誤

完整程式

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <array>
#include <tuple>
#include <functional>
#include <limits>
#include <cstdint>
#include <cctype>
#include <utility>
using namespace std;

int main() {
    int t;
    cin >> t;
    for (int tc = 1; tc <= t; ++tc) {
        string s;
        cin >> s;
        vector<int> y;
        string glyph;
        int h = 0;
        for (char c : s) {
            if (c == 'F')
                --h;
            y.push_back(h);
            glyph.push_back(c == 'R' ? '/' : c == 'F' ? '\\' : '_');
            if (c == 'R')
                ++h;
        }
        int low = *min_element(y.begin(), y.end()), high = *max_element(y.begin(), y.end());
        cout << "Case #" << tc << ":\n";
        for (int row = high; row >= low; --row) {
            string line = "| ";
            for (int col = 0; col < int(s.size()); ++col)
                line += y[col] == row ? glyph[col] : ' ';
            while (line.back() == ' ')
                line.pop_back();
            cout << line << '\n';
        }
        cout << '+' << string(s.size() + 2, '-') << "\n\n";
    }
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多