---
# System prepended metadata

title: 'd139: Compressed String'
tags: [YTP/APCS 練習題單]

---

---
tags: YTP/APCS 練習題單
---
# d139: Compressed String
link: https://zerojudge.tw/ShowProblem?problemid=d139
```cpp=
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    while(cin >> s)
    {
        char tmp = s[0];
        int cnt = 1;
        s[s.length()] = '#';
        for(int i = 1;i < s.length() + 1;i ++){
            char now = s[i];
            if (now == tmp){
                cnt ++;
            }else{
                if (cnt == 1){
                    cout << tmp;
                }
                if (cnt == 2){
                    cout << tmp << tmp;
                    cnt = 1;
                }
                if (cnt >= 3){
                    cout << cnt << tmp;
                    cnt = 1;
                }
            }
            tmp = s[i];
        }
        cout <<endl;
    }
}
```