// 演算法 / Algorithm:
// 對每個單字累加字元權重，% 26 得 0..25，再用 'z'-value 反向對應成字母並串接。
// For each word, sum its letter weights, take % 26, map via 'z'-value, and append.
class Solution {
public:
    string mapWords(vector<string>& words, vector<int>& weights) {
        string result;                     // 用 string 自動管理長度，免去手動配置記憶體
                                           // string auto-manages length; no manual malloc needed.
        result.reserve(words.size());      // 預留空間避免多次重新配置 / reserve to avoid reallocations

        // range-for：依序取出每個單字的參考 w，避免複製
        // range-based for loop: bind each word to reference w (no copy).
        for (const string& w : words) {
            int sum = 0;                   // 這個單字的權重總和 / this word's weight sum

            // 再用 range-for 走過單字裡的每個字元 c / iterate each char c of the word
            for (char c : w) {
                // c - 'a' 轉成 0..25 索引去查權重 / index 0..25 into weights via c - 'a'
                sum += weights[c - 'a'];
            }

            int value = sum % 26;          // 折進 0..25 / fold into 0..25
            // 反向對應：value=0 -> 'z'，value=25 -> 'a'；push_back 把字元接到字串尾端
            // Reverse map: 0 -> 'z', 25 -> 'a'; push_back appends the char to the string.
            result.push_back(static_cast<char>('z' - value));
        }

        return result;                     // string 會自動處理結尾，直接回傳即可 / return directly
    }
};
