网易——字符串编码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_27935693/article/details/60140315
给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDAA会被编码成4A1B2C1D2A。 
输入描述:
每个测试输入包含1个测试用例
每个测试用例输入只有一行字符串,字符串只包括大写英文字母,长度不超过10000。


输出描述:
输出编码后的字符串

输入例子:
AAAABCCDAA

输出例子:

4A1B2C1D2A

#include<stdio.h>
#include<iostream>
using namespace std;
int main(){
    string s;
    cin>>s;
    string ss;
    int j=0;
    string::iterator first=s.begin(),last=s.begin();
    while (last!=s.end()){
        int n=0;
        while(*first==*last&&last!=s.end()){
            ++n;
            ++last;
        }
       cout<<n<<*first;
       first=last;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/sinat_27935693/article/details/60140315