Sequence Decoding

Sequence Decoding

题目描述:
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] to denote the repeated sequence of k times sequence S,where 2 ≤ k ≤ 9. A legal sequence is defined as following.
·A sequence consists of only one character ‘H’ or ‘P’ is a legal sequence.
·Let S1 and S2 be legal sequences. Then the sequence concatenated by S1 and S2 is also a legal sequence.
·Let S be a legal sequence. Then the sequence k[S] is also a legal sequence, where 2 ≤ k ≤ 9.
For example, PHPHPHPH is encoded as 4[PH]. Note that a repeated sequence may contains repeated sequences recursively such as 2[PH4[P]4[H]].
Given a nonempty encoded protein sequence S, your job is to expand S to its original sequence.
That is, you should expand 2[PH4[P]4[H]] to PHPPPPHHHHPHPPPPHHHH.
输入:
The first line is an integer n indicating the number of test cases. Each of the next n lines consists of a legal sequence composed by number digits ‘2’~’9’, ‘[’, ‘]’, ‘P’ and ‘H’.
输出:
For each test case, output the expanding sequence in one line.
样例输入:
3
PHPHP
2[3[P]H2[P]]
HH2[P3[H]]P
样例输出:
PHPHP
PPPHPPPPPHPP
HHPHHHPHHHP
提示:
·1 ≤ n ≤ 10.
·All the inputs are legal.
·The length of each input sequence is less than 50.
·The length of each expanded sequence is less than 1000.
题目大意:
给n个字符串,从内而外的将中括号打开,写出该字符串的完整形式。
一个基础的递归,用指针记录一下位置即可(这道题我就是来记录一下指针的用法,因为经常忘。。。如int p,则在调用函数时,里面应为&p,而定义函数时,里面应为*p,函数体内,*p是p的值,而p是p的地址)。

#include<bits/stdc++.h>
#define maxn 500010
using namespace std;
typedef long long ll;
string str;
string str1;
int len;
int p=0;
string fun(int i,int v,int *p)//从第i个开始遍历,重复了v遍,目前遍历到了p位置
{
    int vv=0;
    string str2="";
    for(int j=i+1; j<len;)
    {
        if(str[j]>='A'&&str[j]<='Z')
        {
            str2+=str[j];
            j++;
            (*p)++;
        }
        else if(str[j]>='0'&&str[j]<='9')
        {
            vv=str[j]-'0';
            j++;
            (*p)++;
        }
        else if(str[j]==']')
        {
            (*p)++;
            break;
        }
        else if(str[j]=='[')
        {
            int pp=0;
            str2+=fun(j,vv,&pp);
            j+=(pp+1);
            (*p)+=(pp+1);
        }
    }
    string str3="";
    for(int j=0; j<v; j++)
        str3+=str2;
    return str3;
}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        cin>>str;
        int v=0;
        len=str.size();
        for(int i=0; i<len;)
        {
            if(str[i]>='A'&&str[i]<='Z')
            {
                str1+=str[i];
                i++;
            }
            else if(str[i]>='0'&&str[i]<='9')
            {
                v=str[i]-'0';
                i++;
            }
            else if(str[i]=='[')
            {
                int p=0;
                str1+=fun(i,v,&p);
                i+=(p+1);
 
            }
        }
        cout<<str1<<endl;
        str1="";
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值