描述

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. 深度学习原理与框架-神经网络-cifar10分类(代码) 1.np.concatenate(进行数据串接) 2.np.hstack(将数据横着排列) 3.hasattr(判断.py文件的函数是否存在) 4.reshape(维度重构) 5.tanspose(维度位置变化) 6.pickle.load(f文件读入) 7.np.argmax(获得最大值索引) 8.np.maximum(阈值比较)

    横1. np.concatenate(list, axis=0) 将数据进行串接,这里主要是可以将列表进行x轴获得y轴的串接 参数说明:list表示需要串接的列表,axis=0,表示从上到下进行串接 ...

  2. 机器学习进阶-直方图与傅里叶变换-傅里叶变换(高低通滤波) 1.cv2.dft(进行傅里叶变化) 2.np.fft.fftshift(将低频移动到图像的中心) 3.cv2.magnitude(计算矩阵的加和平方根) 4.np.fft.ifftshift(将低频和高频移动到原来位置) 5.cv2.idft(傅里叶逆变换)

    1. cv2.dft(img, cv2.DFT_COMPLEX_OUTPUT) 进行傅里叶变化 参数说明: img表示输入的图片, cv2.DFT_COMPLEX_OUTPUT表示进行傅里叶变化的方法 ...

  3. WebAssembly相关

    git搜索:https://github.com/search?q=WebAssembly 相关demo:https://github.com/jpmorganchase/perspective we ...

  4. BBS-文章详情页、点赞功能

    文章详情页--布局中header和左边区域不变--用到继承 home_site和article_detail只是布局 中心区域 只是右侧不同-----用到继承原理 -------- url # 文章详 ...

  5. 创建pod步骤

    创建pod步骤 Steps: pod lib create YBUtils  //创建基本目录结构及工程 pod lib lint YBUtils.podspec //验证podspec文件是否合法 ...

  6. Zabbix3.0版Graphtree的安装配置

    Graphtrees:  https://github.com/OneOaaS/graphtrees 如果是采用yum安装的zabbix-server, 则使用以下方式: # mv /usr/shar ...

  7. 使用Python抓取猫眼近10万条评论并分析

    <一出好戏>讲述人性,使用Python抓取猫眼近10万条评论并分析,一起揭秘“这出好戏”到底如何? 黄渤首次导演的电影<一出好戏>自8月10日在全国上映,至今已有10天,其主演 ...

  8. scrapy 常用代码

    一,scrapy请求 yield scrapy.Request(url=url, dont_filter=True, callback=self.page, meta={'item': item}) ...

  9. JS----addEventListener()

    addEventListener() 用于向指定元素添加事件. 可以向一个元素添加多次事件或者多次不同事件,后面的事件是不会覆盖前面的. 语法: element.addEventListener(ev ...

  10. vc for python2.7

    https://www.microsoft.com/en-us/download/confirmation.aspx?id=44266