---
# System prepended metadata

title: Eb Alto Saxophone Player(UVA10415)
tags: [CPE一顆星必考題]

---

# Eb Alto Saxophone Player(UVA10415) 
## [程式繳交區](https://hackmd.io/@Renektonn/B1ZLex1tye/edit)
## 題目
[點我](https://onlinejudge.org/external/104/10415.pdf)

## 解題網站
[UVA](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1356)
[ZJ](https://zerojudge.tw/ShowProblem?problemid=e531)

## 演算法
```
/*
1. 輸入t
2. 重複t次，若t為0，則停止
    2.1 輸入一行到str
    2.2 宣告陣列lastPress,sum，長度為11，初始值為0
    2.3 掃過str
        2.3.1 對於每個在str中的c，呼叫函式getPress(c)，獲得陣列press
        2.3.2 比較lastPress與press，若lastPress[j] == 0 and press[j] == 1，sum[j]++
        2.3.3 lastPress = press
    2.4 輸出sum
*/
```
:::warning
記得處理cin, getline問題
:::

## 程式碼
```cpp=
#include <bits/stdc++.h>
#define int long long int
using namespace std;

void getPress (char c, int press[]) {
    if (c == 'c') {
        press[2] = press[3] = press[4] = press[7] = press[8] = press[9] = press[10] = 1;
        return;
    } 
    if (c == 'C') {
        press[3] = 1;
        return;
    }
    if ('A' <= c and c <= 'Z') {
        c += 32;
        press[1] = 1;
    }
    switch(c) {
        case 'd':
            press[2] = press[3] = press[4] = press[7] = press[8] = press[9] = 1;
            break;
        case 'e':
            press[2] = press[3] = press[4] = press[7] = press[8] = 1;
            break;
        case 'f':
            press[2] = press[3] = press[4] = press[7] = 1;
            break;
        case 'g':
            press[2] = press[3] = press[4] = 1;
            break;
        case 'a':
            press[2] = press[3] = 1;
            break;
        case 'b':
            press[2] = 1;
            break;
    }
}

signed main() {
    int t;
    cin >> t;
    string str;
    getline(cin, str);
    while (t--) {
        getline(cin, str);
        int lastPress[11] = {}, sum[11] = {};
        for (int i = 0; i < str.length(); i++) {
            char c = str[i];
            int press[11] = {};
            getPress(c, press);
            for (int j = 1; j <= 10; j++) {
                if (lastPress[j] == 0 and press[j] == 1) {
                    sum[j]++;
                }
            }
            for (int j = 1; j <= 10; j++) {
                lastPress[j] = press[j];
            }
        }
        for (int j = 1; j <= 9; j++) {
            cout << sum[j] << " ";
        }
        cout << sum[10] << endl;
    }
    return 0;
}

```