- 1000ms
- 262144K
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]k[S]k[S] to denote the repeated sequence of kkk times sequence SSS, where 2≤k≤92 ≤ k ≤ 92≤k≤9. 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 ≤ 92≤k≤9.
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 ≤ 101≤n≤10.
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;
}
}