题目链接https://pintia.cn/problem-sets/994805342720868352/problems/994805344490864640
题目大意:给出一个字符串,前一个字符串是对后一个字符串的解释。具体看题吧,内容繁琐但不难。
思路:模拟就完事了。
注意
- 运行算法不是
N
次而是N-1
次,因为第一个字符就已经是第一个字符串了 - 字符串拼接直接用加减号,不要像我原来一样第一个字符用
push_back()
,这样会超时
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;
string procQuery(string str) {
string ret;
int ptr = 0, cnt;
char now;
while (ptr != str.length()) {
now = str[ptr];
cnt = 0;
while (ptr != str.length() && str[ptr] == now) {
ptr++;
cnt++;
}
ret += now + to_string(cnt);
}
return ret;
}
int main() {
string str;
int N;
cin >> str >> N;
for (int i = 1; i < N; i++)
str = procQuery(str);
cout << str;
return 0;
}