描述

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. day05-if-else语句

    1.Python条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块.Python程序语言指定任何非0和非空(null)值为true,0 或者 nu ...

  2. C#中关于@的用法

    1. 加在字符串前面,字符串中的 \ 失去转义符的作用,直接写字符串而不需要考虑转义字符 string path = @"C:\Windows\"; // 如果不加 @,编译会提示 ...

  3. https 学习总结

    最近看了点https 做下总结  面的博客如果没有错误的话,理解起来绝对是醍醐灌顶!让人信服,如果我的理解有问题,请及时指正! 参考博客:    http://www.ruanyifeng.com/b ...

  4. Review CSS Selectors

    Throughout this lesson, you learned how to select HTML elements with CSS and apply styles to them. L ...

  5. Homebrew安装与使用

    Homebrew是什么? 官方解释: Homebrew是以最简单,最灵活的方式来安装苹果公司在MacOS中不包含的UNIX工具. 官网:传送门 Git仓库地址:传送门 Homebrew 怎么安装 ?怎 ...

  6. JS 实现分页打印

    在调用window.print()时,可以实现打印效果,但内容太多时要进行分页打印. 在样式中有规定几个打印的样式 page-break-before和page-break-after CSS属性并不 ...

  7. C++ new 和malloc 区别

    1.分配地方不同,malloc是堆上面,new是自由存储区域 2.malloc/delete是函数,new/delete是操作符,可以重载 3.malloc 要指定大小,返回的是void*指针,开辟的 ...

  8. 【剑指offer】单链表尾部插入一个节点

    #include <iostream> using namespace std; //链表结构体 struct ListNode { int m_Value; ListNode *next ...

  9. CMake Error at cmake/OpenCVUtils.cmake

    CMake Error at cmake/OpenCVUtils.cmake:1047 (message): Failed to download . Status= Call Stack (most ...

  10. js高级-函数变量提升

    var a = 10; function f1(){ console.log(a) //undefined  函数变量提升了 函数执行之前想创建了函数的EC 把函数里面声明的变量初始化undefine ...