首页 > 编程语言 > 详细

C++9018:2325——压缩字符串

时间:2021-04-15 01:16:23      阅读:27      评论:0      收藏:0      [点我收藏+]

题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=2325

题目描述

输入字符串,输出压缩后的字符串。压缩的方法是把连续的相同字母压缩为“长度+字母”的形式,单个的字母不需要压缩。

输入

一行字符串,只包含小写英文字母,长度不超过255。

输出

一行,压缩后的字符串

样例输入

aaabbbbbx

样例输出

3a5bx
作者分析:基础字符串操作
#include <bits/stdc++.h>
using namespace std;

int main(){
    string a;
    int x = 0,len;
    cin >> a;
    len = a.size()-1;
    for (int i = 0;i <= len;i++){
        if (a[i] == a[i+1]) x++;
        else{
            if (x == 0)cout << a[i];
            else{
                cout << ++x << a[i];
                x = 0;
            }
        }
    }
    return 0;
}

C++9018:2325——压缩字符串

原文:https://www.cnblogs.com/linyiweiblog/p/14660084.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!