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.

样例输入

  1. 3
  2. PHPHP
  3. 2[3[P]H2[P]]
  4. HH2[P3[H]]P

样例输出

  1. PHPHP
  2. PPPHPPPPPHPP
  3. 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写了。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e5+;
  4. char s[N];
  5. int a[N],n,T;
  6. void dfs( int L , int R ){
  7. int num = ;
  8. for( int i = L ; i <= R ;i ++ ){
  9. if( s[i] == 'P' || s[i] == 'H' ){
  10. printf("%c",s[i] );
  11. }else if( isdigit(s[i]) ){
  12. int j = i ;
  13. num = ;
  14. while( isdigit(s[j]) && j < n ){
  15. num = num * + s[i] - '' ;
  16. j ++ ;
  17. }
  18. i = j - ;
  19. }else if( s[i] == '[' ){
  20. for( int j = ; j < num ; j++ )
  21. dfs( i+ , a[i] - );
  22. num = ;
  23. i = a[i] ;
  24. }else if( s[i] == ']' ){
  25. continue ;
  26. }
  27. }
  28. }
  29.  
  30. int main()
  31. {
  32. ios_base :: sync_with_stdio(false);
  33. cin.tie(NULL) , cout.tie(NULL) ;
  34. cin >> T ;
  35.  
  36. while(T--){
  37. cin >> s;
  38. n = strlen( s );
  39. stack<int> st;
  40. for( int i = ; i < n ; i++ ){
  41. if( s[i] == '[' ){
  42. st.push(i) ;
  43. }else if( s[i] == ']' ){
  44. int cur = st.top() ;
  45. a[cur] = i ;
  46. st.pop();
  47. }
  48. }
  49. dfs( , n- );
  50. puts("") ;
  51. }
  52. return ;
  53. }

【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. java.util之一:ArrayList

    ArrayList是java中的线性结构的一种表示方法,在java中使用频率非常高,下面来一步一步分析其底层的实现.(JDK1.8) 一.构造函数 ArrayList的构造函数有三个,分别如下, 我们 ...

  2. JAVA基础之访问控制权限

    包:库单元 1.当编写一个Java源代码文件时,此文件通常被称为编译单元(有时也被称为转译单元). 2.每个编译单元都必须有一个后缀名.java,而在编译单元内则可以有一个public类,该类名称必须 ...

  3. python-pptx

    python-pptx的使用首先需要了解几个基本概念: 1.引入python-pptx frompptximportpresentation    # 实例化Presentation    prs= ...

  4. cordova run android 可能遇到的错误解决

    运行: ionic cordova build 等待下载,然后根据提示 输入android或者ios平台,即可 运行cordova run android 报错: 最快捷的解决方法就是使用Androi ...

  5. ISO/IEC 9899:2011 条款6.7.8——类型定义

    6.7.8 类型定义 语法 1.typedef-name: identifier 约束 2.一个typedef名指定了一个可变修改的类型,然后它应该具有语句块作用域. 语义 3.在一个声明中,该声明的 ...

  6. ISO/IEC 9899:2011 条款6.5.17——逗号操作符

    6.5.17 逗号操作符 语法 1.expression: assignment-expression expression    ,    assignment-expression 语义 2.一个 ...

  7. C++ 11 线程调用类的成员函数解决办法

    在C++中,_beginthreadex 创建线程是很麻烦的.要求入口函数必须是类的静态函数. 通常,可以采用thunk,或者模板来实现. 因C++ 11中引入了 std::async ,可以很好的解 ...

  8. JS 数字相加出现多个小数的问题

    今天在页面上用到了js进行小数相加119.01+0.01,结果大家都知道应该是:119.02的,然而结果是119..0200000…. ,莫名其妙的,还以为是我写的程序有问题,后来查了下才知道这是ja ...

  9. 阿里云服务器Svn-Server无法连接

    总结:关于阿里云服务器Svn-Server无法连接,Svn-Server的配置问题 2018年07月09日 11:51:08 周同学的博客 阅读数:355   最近在使用阿里云服务器时,SQL SER ...

  10. Elasticsearch服务器开发(第2版).pdf 含目录

    Elasticsearch服务器开发(第2版) 介绍: <Elasticsearch服务器开发(第2版)>这一版针对Elasticsearch的新版本更新了内容,增加了第1版中遗漏的重要内 ...