一、题目概述
略;
二、思路
统计连续出现的字符数,字符串最后一位作哨兵。
注意使用string不要使用 string = string + *的方式,回两次调用string,时空代价很高。使用+=很好。
三、代码
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int N;
cin >> str >> N;
for( int i = 1; i < N; ++i )
{
string temp;
for( int j = 1, cnt = 1; j <= str.size(); ++j )
if( j == str.size() || str[j] != str[j - 1] )
{
temp += str[j - 1] + to_string(cnt);
cnt = 1;
}
else ++cnt;
str = temp;
}
cout << str;
}