网易编程题:连续字符转换

来源:互联网 发布:中日关系未来走向 知乎 编辑:程序博客网 时间:2024/06/12 09:11

出自:2017网易实习编程题


题目:

给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDAA会被编码成4A1B2C1D2A。 

输入描述:
每个测试输入包含1个测试用例每个测试用例输入只有一行字符串,字符串只包括大写英文字母,长度不超过10000。


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

输入例子:
AAAABCCDAA

输出例子:
4A1B2C1D2A

代码:

#include <iostream>#include<string>#include <vector>#include<stdio.h>using namespace std;  int main()    {    string str1;    string str_out;    unsigned int len=0;    unsigned int num=0;    char first;    char next;    while(cin>>str1)        {        len=str1.length();        for(int i=0;i<len;)            {              first=str1[i];              next=str1[++i];              num=1;            while(next==first&&i!=len)                {                next=str1[++i];                ++num;                }            cout<<num<<first;        }                    cout<<endl;               }    return 0;}添加笔记


1 0