Chuỗi Thu Gọn

View as PDF

Submit solution

Points: 0.01 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: THUGON.INP
Output: THUGON.OUT

Author:
Problem type
Allowed languages
C, C++, Go, Java, Kotlin, Pascal, PyPy, Python, Rust, Scratch

Một chuỗi kí tự thuần nhất được định nghĩa là chuỗi chỉ bao gồm các kí tự latinh in hoa từ ~'A'~ đến ~'Z'~. Chuỗi kí tự thuần nhất có thể được thu gọn thành các kí tự kèm theo số lần xuất hiện liên tiếp của kí tự đó (Số lần xuất hiện sẽ đứng ngay trước kí tự, nếu số lần xuất hiện là ~1~ thì không cần ghi số ~1~). Ví dụ: Chuỗi thuần nhất ~AABBCCCDDEAAA~ được thu gọn thành chuỗi ~2A2B3C2DE3A~.

Yêu cầu: Đọc các chuỗi thuần nhất từ tệp dữ liệu THUGON.INP có độ dài không quá ~255~ kí tự và chuyển thành chuỗi thu gọn.

Input

Từ tệp văn bản THUGON.INP gồm nhiều dòng, mỗi dòng là một chuỗi thuần nhất.

Output

Đưa ra tệp văn bản THUGON.OUT gồm nhiều dòng, mỗi dòng tương ứng là chuỗi thu gọn thỏa mãn yêu cầu đề bài.

Sample Input 1

AABBCCCDDEAAA
ABBBBBDDEE

Sample Output 1

2A2B3C2DE3A
A5B2D2E

Comments

Please read the guidelines before commenting.



  • 0
    tranductrongvip12411  commented on Feb. 10, 2026, 2:23 p.m.

    include<bits/stdc++.h>

    using namespace std; int main() { iosbase::syncwith_stdio(false); cin.tie(NULL); freopen("THU.inp","r",stdin); freopen("THU.out","w",stdout); string s; while(cin>>s) { long long dem=0; for(int i=0;i<s.size();i++) { if(s[i]==s[i-1])dem++; else dem=1; if(s[i]!=s[i+1]) { if(dem!=1)cout<<dem<<s[i]; else cout<<s[i]; } } cout<<endl; } return 0; } code nay no nhan dung nhung no ghi cai qq j dang sau roi no danh la sai


  • -2
    tathaolqd  commented on Dec. 1, 2025, 1:49 p.m.

    // ██╗ ██╗ █████╗ ██████╗ ████████╗ █████╗ ████████╗ // ██║ ██║██╔══██╗██╔═══██╗ ╚══██╔══╝██╔══██╗╚══██╔══╝ // ███████║███████║██║ ██║ ██║ ███████║ ██║ // ██╔══██║██╔══██║██║ ██║ ██║ ██╔══██║ ██║ // ██║ ██║██║ ██║╚██████╔╝ ██║ ██║ ██║ ██║ // ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝

    include <bits/stdc++.h>

    define endl '\n'

    define maxn 100005

    define MOD 100000000000007

    define Task "THUGON"

    define pb push_back

    define int long long

    define gr greater<int>()

    define lg length()

    define us unsigned long long

    define fi first

    define se second

    pragma GCC target ("avx2")

    pragma GCC optimize ("O3")

    pragma GCC optimize ("unroll-loops")

    pragma GCC optimize ("O2")

    pragma GCC optimize ("Os")

    using namespace std; const long long MOD1=1000000005; const long long MOD2=1000000007; const long long MOD3=1000000009; signed main() { iosbase::syncwith_stdio(false); cin.tie(NULL); if(fopen(Task".inp","r")){ freopen(Task".inp", "r", stdin); freopen(Task".out", "w", stdout); } string s; while(cin>>s) {

    int d=1;
    string res="";
    int j=1,i=0;
    while(i&lt;s.lg)
    {
       if(s[i]==s[i+1])
       {
           d++;
       }else{
            if(d>1)
            {
                 res+=to_string(d);
            }
    
        res+=s[i];
        d=1;
       }
       i++;
    }
    cout<&lt;res&lt;&lt;endl;
    }
    
    return 0;
    

    }


  • -3
    thanngocmai  commented on Aug. 5, 2025, 3:58 a.m.

    nếu dãy là ABABAB thì có thành 3AB ko ạ, sao code này của t bị WA nhỉ:

    include<bits/stdc++.h>

    using namespace std; using ll = long long; const ll maxn = 1e6 + 5;

    string s, res; ll n; int main() { iosbase::syncwith_stdio(0); cin.tie(0); cout.tie(0);

    freopen("THUGON.inp","r",stdin);
    freopen("THUGON.out","w",stdout);
    
    while(cin>>s)
    {
        n = s.size();
        s = " "+s;
        res = "";
        for(int i=1; i&lt;n; i++)
        {
            ll dem = 1;
            while(s[i] == s[i+1] && i+1 <=n)
            {
                dem++;
                i++;
            }
            if(dem >1)
            res += to_string(dem);
            res += s[i];
        }
        cout<&lt;res&lt;&lt;'\n';
    
    }
    

    }


    • 1
      minhkhangvophuoc  commented on Aug. 28, 2025, 8:34 a.m. edit 2

      0 có 3AB từ ABABAB đâu, cùng lắm nếu muốn 3AB thì phải từ AAAB -> 3AB mới đúng. Đếm từng kí tự thôi 0 gộp lại.

      Còn nữa: cout<&lt;res<<'\n'; sai cú pháp.


    • 0
      haoluu2009  commented on Aug. 28, 2025, 7:40 a.m. edited

      bạn đang dùng 1-based kìa, thì vòng for phải là for(int i = 1; i <= n; i++). Nma nếu dùng for thì mỗi lần duyệt xong i lại tăng lên 1 đơn vị sẽ bị duyệt lặp lại đó, bạn dùng while thử xem. Với cả sau khi cộng dồn vào rồi thì dịch chuyển i -> j để tránh lặp. Code: https://ideone.com/uftNHO


  • 0
    VoTheHao99  commented on July 2, 2025, 2:30 p.m. edited

    nếu mà bài ko nhập số n chuỗi thì nhập tới khi nào v mn?


    • 1
      SmileyBoi  commented on July 6, 2025, 2:11 a.m.

      string s; while(cin >> s){ //code }


  • -15
    nguyencongquang0209  commented on April 27, 2025, 8:12 a.m. edit 2

    This comment is hidden due to too much negative feedback. Show it anyway.