2 条题解

  • -2
    @ 2024-10-10 21:55:05

    题解如下:

    #include<bits/stdc++.h>
    #define ULL unsigned long long
    #define LL long long
    #define endl '\n'
    #define debug(a) cout<<#a<<"="<<a<<endl;
    #define PII pair<int,int>
    using namespace std;
    const int N =  1 *1e7 + 10,M = 5 * 1e3 + 10,inf = 0x3f3f3f3f;
    
    string str,ans;
    string getstr(int n) //将整形数字转化为字符串操作
    {
        string t;
        while (n)
        {
            t += n % 10 + '0';
            n /= 10;
        }
        reverse(t.begin(),t.end());
        return t;
    }
    void solve()
    {
        cin>>str;
        for(int i=0;i<str.size();)
        {
            int j = i , cnt = 0; 
          //cnt 记录从当前字母开始有多少个重复字母
            while(str[i]==str[j] && j < str.size()) j++,cnt++; 
          //如果相同就再加,再往下找,直到找到一个不同的
            if(cnt ==  1) ans += str[i]; //只有一个就是没有重复字母,答案只加上该字母
            else
            {
                ans += str[i];
                ans += getstr(cnt); 
                //否则先加字母再加上重复相同字母的个数(可能超过10个,所以不能用 ans += cnt + '0')
                // 相同的,可以调用函数库里的to_string直接转换 -> ans += to_string(cnt);
            }
            i = j;
        }
        if(ans==str) cout<<"NO"<<endl; //如果答案还是跟以前一样,就是没被压缩
        else cout<<ans<<endl;
    }
    int main()
    {
        ios::sync_with_stdio(0);cin.tie(nullptr),cout.tie(0);
        int _=1;
        // cin>>_;
        while(_--){solve();}
        return 0;
    }
    
    /**
     *    author: Nijika_jia
     *    created: 2024.10.09 21:08:27
     */
    
    
    ```
    
    
    ```

    信息

    ID
    5401
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    18
    已通过
    5
    上传者