个人练习-PAT甲级-1140 Look-and-say Sequence

题目链接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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值