描述

Our strings only contain letters(maybe the string contains nothing).

Now we define the production as follows:

1. (C) --> C

2. C --> C

3. (C:num)-->repeat C num times.

Illustration: (C) or C stands for a string only contains letters. (C:num) means that we should repeat string C num times (1<=num<=9).

For example: (((ab)(cd:2)):2) --> abcdcdabcdcd.

If the length of result exceed 2^20 characters print "Too Long!" in one line.

输入

There are multiple test cases. The first is a positive integer T, indicating the number of test cases.

For each test case, there is only one line contains a justifiable string.

输出

Print the result after converting. If the length of result exceed 2^20 characters print "Too Long!" in one line.

样例输入

7
(abc)
(abc:5)
((ab)(cd:2))
(((ab)(cd:2)):2)
()
(aa(bb:3)(cc:2))
(((((((((((((((((((((((uNVZgs:2):2):2):2):2):2):2):2):2):2):2):2):2):2):2):2):2):2):2):2):2):2):2)

样例输出

abc
abcabcabcabcabc
abcdcd
abcdcdabcdcd

aabbbbbbcccc
Too Long!

题意

1. (C) --> C

2. C --> C

3. (C:num)-->repeat C num times.

题解

类似于括号匹配,如果出现右括号就把中间的字符串接起来重新插入

代码

 #include<bits/stdc++.h>
using namespace std; const int maxlen=; int main()
{
int T;
cin>>T;
while(T--)
{
stack<string>st;
int flag=;
string s1="",s;
cin>>s;
for(int i=;i<(int)s.size();i++)
{
if(s[i]=='(')
{
if(!s1.empty())st.push(s1);
s1="(";
st.push(s1);
s1.clear();
}
else if(s[i]==')')
{
string s2="";
if(!s1.empty())st.push(s1);
s1.clear();
while(st.top()!="(")
{
s2.insert(,st.top());
st.pop();
}
st.pop();
st.push(s2);
}
else if(s[i]==':')
{
if(!s1.empty())st.push(s1);
int sum=s[++i]-'';
if((int)st.top().size()*sum>maxlen)
{
flag=;
break;
}
s1=st.top();st.pop();
string s3="";
while(sum--)s3+=s1;
st.push(s3);
s1.clear();
}
else
s1+=s[i];
}
if(!flag||(int)st.top().size()>maxlen)cout<<"Too Long!\n";
else cout<<st.top()<<'\n';
}
return ;
}

TZOJ 4621 Grammar(STL模拟)的更多相关文章

  1. stl+模拟 CCF2016 4 路径解析

    // stl+模拟 CCF2016 4 路径解析 // 一开始题意理解错了.... #include <iostream> #include <string> #include ...

  2. 【STL+模拟】UVa 506 - System Dependencies

    System Dependencies  Components of computer systems often have dependencies--other components that m ...

  3. STL——模拟实现空间配置器

    目录 问题 SGI版本空间配置器-std::alloc 一级空间配置器 二级空间配置器 Refill.chunkAlloc函数 最后,配置器封装的simple_alloc接口 问题 我们在日常编写C+ ...

  4. UVA - 11995 - I Can Guess the Data Structure! STL 模拟

    There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...

  5. CCF 201403-3 命令行选项 (STL模拟)

    问题描述 请你写一个命令行分析程序,用以分析给定的命 令行里包含哪些选项.每个命令行由若干个字符串组成,它们之间恰好由一个空格分隔.这些字符串中的第一个为该命令行工具的名字,由小写字母组成,你的程序 ...

  6. CCF 201403-2 窗口 (STL模拟)

    问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示位于顶层的窗口里的 ...

  7. uva 327 Evaluating Simple C Expressions 简易C表达式计算 stl模拟

    由于没有括号,只有+,-,++,--,优先级简单,所以处理起来很简单. 题目要求计算表达式的值以及涉及到的变量的值. 我这题使用stl的string进行实现,随便进行练手,用string的erase删 ...

  8. HDU5071 - Chat(STL模拟)

    题目描述 略... 题解 现场赛的时候真是脑残...用splay去写..写完发现调试不出来...然后才发现数据范围才5000...不过那时候只有40分钟了..用数组模拟了速度敲了一发.写完只剩10几分 ...

  9. 牛客练习赛31 D 神器大师泰兹瑞与威穆 STL,模拟 A

    牛客练习赛31 D 神器大师泰兹瑞与威穆 https://ac.nowcoder.com/acm/contest/218/D 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 26214 ...

随机推荐

  1. 03_JSX理解和使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 与前端对接 jsonp

    主要是回调的写法,前端人员接受的数据格式      参数 (jsonString);

  3. win7下iis的配置问题

    开始asp的学习时,首先得配置iis服务,中途遇到各种问题也谷歌了不少,但是答案都很凌乱,折腾了两天才彻底解决这个问题.下面是我坎坷的配置过程,希望对你有所帮助: 第一步:打开Internet信息服务 ...

  4. Cookie安全小结

    Cookie机制:一般来说,同域内浏览器中发出的任何一个请求都会带上Cookie,无论请求什么资源,请求时,Cookie出现在请求头的Cookie字段中.服务端响应头的Set-Cookie字段可以添加 ...

  5. 一个linux内核模块移植到低版本时发生的异常

    在3.10的内核版本下,有一个运行稳定的内核模块,移植到suse11的时候,编译正常,运行则直接出现crash: <>[ <>[ 503.347300] Modules lin ...

  6. 【384】reduce归纳、map映射、filter筛选 的用法

    参考:4. Map, Filter and Reduce — Python Tips 0.1 documentation 参考:Python的functools.reduce用法 Map:映射,对于列 ...

  7. 重建redo文件

    需求背景 由于前期安装oracle时redo文件大小或者路径规划不合理需要进行修改,以便满足性能测试要求.redo文件规划大小建议与生产环境一致. 重做日志相关数据字典 1.v$log  记录数据库中 ...

  8. TCP/IP网络协议的通俗理解,SOCKET,HTTP,SOAP

    TCP/IP,HTTP,SOAP等协议之区别   术语TCP/IP代表传输控制协议/网际协议,指的是一系列协议.“IP”代表网际协议,TCP和UDP使用该协议从一个网络传送数据包到另一个网络.把IP想 ...

  9. unarjs识别组建compoment

    http://stackoverflow.com/a/28210364/1070244 判断自定义元素,是否为未知元素 chrome和firefox存在差异,尤老 研究的代码 // Chrome re ...

  10. 导入maven框架报错

    原因pom文件第一行报错X org.apache.maven.archiver.mavenarchiver.getmanifest怎么解决 解决: 原因就是你的maven的配置文件不是最新的 1.he ...