2017网易游戏雷火盘古实习生招聘笔试:字符串编码

    xiaoxiao2021-03-25  98

    题目来源:伯乐在线

    题目:

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

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

    输出描述:

    输出编码后的字符串

    输入例子:

    AAAABCCDAA

    输出例子:

    4A1B2C1D2A

    代码:

    #include <iostream> #include <cstring> #include <queue> using namespace std; int main( ) { freopen("/home/liyuanshuo/ClionProject/hihocoder/in.in", "r" , stdin ); queue<char> tmp; char c; while ( cin>>c ) { tmp.push( c ); } while ( tmp.size() ) { int num = 1; c = tmp.front(); tmp.pop(); while ( c == tmp.front() ) { num++; tmp.pop(); } cout<<num<<c; } cout<<endl; return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-19668.html

    最新回复(0)