題目: UVa - 10415 - Eb Alto Saxophone Player

題目說明

給薩克斯風的各種音調的指法,及由這些音調組成的歌曲 ( 假設一個手指頭只能按特定一個按鍵,且不同手指控制不同按鈕 ),求每根手指需要按下的次數,如果某一按鍵在下一音符時不會用到,那麼就會放開,否則就是維持按著的情況。

Input: 第一個整數 T,表示有 T 組測資,後面 T 行每行為一首歌曲,歌曲可能為空。

Output: 對於每組測資,請輸出 10 個數字,代表每個手指的按下次數,數字用空格分隔。

參考自:e531. 10415 - Eb Alto Saxophone Player - 高中生程式解題系統

解題思路

先建表,建立音調及手指的映射,之後查表即可,需要注意的是由於歌曲可能為空,所以使用 getline() 讀取,還需要紀錄上一個音調按下的手指,並在計算次數時比對。

參考解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>

using namespace std;

static auto __ = []
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 0;
}();

int cnt[10];
string present;
unordered_map<char, string> dict
{
{'c', "0111001111"},
{'d', "0111001110"},
{'e', "0111001100"},
{'f', "0111001000"},
{'g', "0111000000"},
{'a', "0110000000"},
{'b', "0100000000"},
{'C', "0010000000"},
{'D', "1111001110"},
{'E', "1111001100"},
{'F', "1111001000"},
{'G', "1111000000"},
{'A', "1110000000"},
{'B', "1100000000"}
};

void init()
{
fill(cnt, cnt + 10, 0);
present.assign(10, '0');
}

void print()
{
for (int i = 0; i < 10; ++i)
{
if (i) cout << ' ';
cout << cnt[i];
}
cout << '\n';
}

int main()
{
int T;
(cin >> T).ignore();

string str;
while (T--)
{
init();
getline(cin, str);

for (auto& ch : str)
{
for (int i = 0; i < 10; ++i)
cnt[i] += max(0, dict[ch][i] - present[i]);
present = dict[ch];
}

print();
}
}

參考資料

e531. 10415 - Eb Alto Saxophone Player - 高中生程式解題系統