UVa10415

題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&page=show_problem&problem=1356

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
#include <iostream>
#include <vector>
#include <map>

using namespace std;

int main(){
int t;
cin >> t >> ws;

// 用map對應 pair<char, vector<int>> 的方式 ,每個char有自己的vector指法
map<char, vector<int>> m;

m['c'] = {0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1};
m['d'] = {0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0};
m['e'] = {0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0};
m['f'] = {0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0};
m['g'] = {0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0};
m['a'] = {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0};
m['b'] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0};
m['C'] = {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0};
m['D'] = {0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0};
m['E'] = {0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0};
m['F'] = {0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0};
m['G'] = {0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0};
m['A'] = {0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0};
m['B'] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};

while(t--){
// 初始化prev(上一個指法)與ans(結束時輸出的按的次數)
int prev[11] = {0};
int ans[11] = {0};

string s;
getline(cin, s);

for(char sound : s){
// 對於每一個音符,檢查他的每一個需要按的位置,如果需要按且上一個沒按到,就要++ans
for(int j = 1; j<=10; ++j){
if(m[sound][j] == 1){
if(prev[j] == 0){
++ans[j];
prev[j] = 1;
}
}
else prev[j] = 0;
}
}

// 輸出
for(int i=1; i<=10; ++i){
cout << ans[i] << (i==10 ? '\n' : ' ');
}
}
return 0;
}