网易游戏实习编程题

来源:互联网 发布:五毛特效app软件 编辑:程序博客网 时间:2024/04/27 22:04

1.给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDAA会被编码成4A1B2C1D2A。
输入描述:
每个测试输入包含1个测试用例
每个测试用例输入只有一行字符串,字符串只包括大写英文字母,长度不超过10000。

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

#include<iostream>#include<string>#include<vector>#include<cstdio>#include<sstream>using namespace std;int main(){    string s;    char  temp;    int t;    vector<string> ivstr;    while (getline(cin,s))    {        temp = s[0];        t = 0;        size_t i;        for ( i = 0; i<s.length(); ++i)        {            if (s[i] == temp) t++;            else            {                   ivstr.push_back(to_string(t));                t = 1;                 string ss;                stringstream stream;                stream << s[i-1];                ss = stream.str();                ivstr.push_back(ss);                temp = s[i];            }        }        ivstr.push_back(to_string(t));        string ss;        stringstream stream;        stream << temp;        ss = stream.str();        ivstr.push_back(ss);        for (int i = 0; i < ivstr.size();++i)            cout <<ivstr[i];        cout << endl;    }}

2.

原创粉丝点击