National Contest for Private Universities (NCPU), 2019 F. Sequence Decoding

编辑代码
  • 1000ms
  • 262144K

The amino acids in proteins are classified into two types of elements, hydrophobic (nonpolar) and hydrophilic (polar). Hydrophobic and hydrophilic are denoted by H and P respectively. A protein is represented by a sequence of H and P such as PPHHHPHPH. In order to reduce the representation length of a sequence, we use the notation k[S]k[S]k[S] to denote the repeated sequence of kkk times sequence SSS, where 2≤k≤92 ≤ k ≤ 92k9. A legal sequence is defined as following.

A sequence consists of only one character 'H' or 'P' is a legal sequence.

Let S1S_1S1 and S2S_2S2 be legal sequences. Then the sequence concatenated by S1S_1S1 and S2S_2S2 is also a legalsequence.

Let SSS be a legal sequence. Then the sequence k[S]k[S]k[S] is also a legal sequence, where 2≤k≤92 ≤ k ≤ 92k9

For example, PHPHPHPH is encoded as 4[PH]. Note that a repeated sequence may contain srepeated sequences recursively such as 2[PH4[P]4[H]].

Given a nonempty encoded protein sequence SSS, your job is to expand SSS to its original sequence.That is, you should expand 2[PH4[P]4[H]] to PHPPPPHHHHPHPPPPHHHH.

Input Format

The first line is an integer nnn indicating the number of test cases. Each of the next nnn lines consistsof a legal sequence composed by number digits '2'~'9', '[', ']', 'P' and 'H'.

Output Format

For each test case, output the expanding sequence in one line.

Technical Specification

1≤n≤101 ≤ n ≤ 101n10.

All the inputs are legal.

The length of each input sequence is less than 505050.

The length of each expanded sequence is less than 100010001000.

样例输入 复制
3
PHPHP
2[3[P]H2[P]]
HH2[P3[H]]P
样例输出 复制
PHPHP
PPPHPPPPPHPP
HHPHHHPHHHP

题意:给你一段字符串只含 【,】,H,P,和数字,要把这些数字和中括号里的字母展开复制多少遍根据中括号外面的数字决定。这个深搜搜的我有点那啥,这个深搜的形式和以前的有些不同这个是更新的位置,一开始正为这个如何更新位置发愁对我来说也是一种新的深搜形式吧。

#include <bits/stdc++.h>
using namespace std;
char sv[10002];
int dfs(int w){
    int k=0;


    while(1){
    int p;
    k=0;
    while(sv[w]>='0'&&sv[w]<='9')
    {
        k=k*10+sv[w]-'0';
        w++;
    }
    k=k==0?1:k;
    if(sv[w]=='[')
    {
        for(int u=1;u<=k;u++)
            p=dfs(w+1);
        w=p;
    }
    else if(sv[w]=='P'||sv[w]=='H')
    {
        cout<<sv[w];
        w++;
    }
    else
        return w+1;

    }



}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        scanf("%s",sv);
        dfs(0);
        cout<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值