Codeforces Gym 101164 D. Reading Digits (模拟)

题意

编码方式定义为: encodes “1211” as: “one of one, one of two, two of one”, or, more precisely: “111221”.

二次编码为: encoding of “111221” string is: “312211”.

保证不存在待编码串联系相同字符在 10 个以上的。

给定 k 次编码后的字符串,求原串的 pos 位的字符

解题思路

由于不知道每次编码后的串长,故用 string 。简单模拟解码过程。

代码

#include 
using namespace std;
int k, pos;
string src, decode;
int main()
{
    scanf("%d %d", &k, &pos);
    cin>>src;
    for(int i=k;i;i--)
    {
        decode = "";
        for(int idx=0, repeat, num;idx2)
        {
            repeat = src[idx]-'0',  num = src[idx+1];
            decode += string(repeat, char(num));
        }
        src = decode;
    }
    printf("%c\n", src[pos]);
}

你可能感兴趣的:(Codeforces,Gym)