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.


【题解】:

  一开始想着怎么用栈来模拟,后来写不出来,队友提示用dfs写就好了。

  我就用dfs写了。

 #include<bits/stdc++.h>
using namespace std;
const int N = 1e5+;
char s[N];
int a[N],n,T;
void dfs( int L , int R ){
int num = ;
for( int i = L ; i <= R ;i ++ ){
if( s[i] == 'P' || s[i] == 'H' ){
printf("%c",s[i] );
}else if( isdigit(s[i]) ){
int j = i ;
num = ;
while( isdigit(s[j]) && j < n ){
num = num * + s[i] - '' ;
j ++ ;
}
i = j - ;
}else if( s[i] == '[' ){
for( int j = ; j < num ; j++ )
dfs( i+ , a[i] - );
num = ;
i = a[i] ;
}else if( s[i] == ']' ){
continue ;
}
}
} int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(NULL) , cout.tie(NULL) ;
cin >> T ; while(T--){
cin >> s;
n = strlen( s );
stack<int> st;
for( int i = ; i < n ; i++ ){
if( s[i] == '[' ){
st.push(i) ;
}else if( s[i] == ']' ){
int cur = st.top() ;
a[cur] = i ;
st.pop();
}
}
dfs( , n- );
puts("") ;
}
return ;
}

【dfs】Sequence Decoding的更多相关文章

  1. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  2. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  3. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. 【dfs】P1331 海战

    题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防海军部仅有很少的几 ...

  5. 【dfs】p1731 生日蛋糕

    1441:[例题2]生日蛋搞 [题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.设从下往上数第i(1≤i≤M)层蛋糕是半径为Ri, 高 ...

  6. 【dfs】LETTERS

    1212:LETTERS [题目描述] 给出一个roe×colroe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母.问最多可以经过几个字母. [ ...

  7. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  8. 【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名

    [题目大意] 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序,约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他 ...

  9. 【DFS】BZOJ3522-[Poi2014]Hotel

    [题目大意] 给出一棵树,求三个节点使得它们两两之间的距离相等,问共有多少种可能性? [思路] 显然,这三个节点是关于一个中心点对称地辐射出去的. 枚举中心点,往它的各个子树跑Dfs.tmp[i]表示 ...

随机推荐

  1. Software Project Management_JUnit && Maven

    Task1: Develop the project “HelloWorld” -A .java program: Just print out “Hello” + your name; -A tes ...

  2. 将WSL2作为生产力工具

    适用于 Linux 的 Windows 子系统 (WSL) 是 Windows 10新增的功能,使用它可以直接在 Windows 上运行 Linux 命令.而WSL 2 是WSL的一个新版本,它支持适 ...

  3. (二)SpringMVC+mybatis实践

    一.需求 以案例作为驱动 springmvc和mybatis使用一个案例(商品订单管理系统) 功能需求:商品的列表查询 二.环境准备 数据库环境 Java环境 jdk1.8 eclipse sprin ...

  4. 扩展和嵌入 Python 解释器 用 C 或 C++ 编写模块以使用新模块来扩展 Python 解释器的功能 定义新的函数\对象类型\方法。 将 Python 解释器嵌入到另一个应用程序中

    // https://python3-cookbook.readthedocs.io/zh_CN/latest/c15/p02_write_simple_c_extension_module.html ...

  5. flutter 中文件工具类

    添加依赖: path_provider: ^0.5.0+1 import 'dart:convert'; import 'dart:io'; import 'package:path_provider ...

  6. 【转】Python常见web系统返回码

    responses = { 100: ('Continue', 'Request received, please continue'), 101: ('Switching Protocols', ' ...

  7. 【转载】 机器学习实战 - 读书笔记(07) - 利用AdaBoost元算法提高分类性能

    原文地址: https://www.cnblogs.com/steven-yang/p/5686473.html ------------------------------------------- ...

  8. 基于ZooKeeper实现简单的服务注册于发现

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Shaun_luotao/article/ ...

  9. QQ第三方登录回调地址的问题

    如题,维护以前的项目,发现原来QQ的第三方登录竟然失败了.回调地址的问题 原来是以前的规则变了.好吧,那就改,谁叫我不是改变规则的人. 中途浪费了点时间,项目很大,我一下也找不到项目里那个接口调用的, ...

  10. 泡泡一分钟:Robust and Fast 3D Scan Alignment Using Mutual Information

    Robust and Fast 3D Scan Alignment Using Mutual Information 使用互信息进行稳健快速的三维扫描对准 https://arxiv.org/pdf/ ...