TZOJ 4621 Grammar(STL模拟)
描述
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模拟)的更多相关文章
- stl+模拟 CCF2016 4 路径解析
// stl+模拟 CCF2016 4 路径解析 // 一开始题意理解错了.... #include <iostream> #include <string> #include ...
- 【STL+模拟】UVa 506 - System Dependencies
System Dependencies Components of computer systems often have dependencies--other components that m ...
- STL——模拟实现空间配置器
目录 问题 SGI版本空间配置器-std::alloc 一级空间配置器 二级空间配置器 Refill.chunkAlloc函数 最后,配置器封装的simple_alloc接口 问题 我们在日常编写C+ ...
- 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. ...
- CCF 201403-3 命令行选项 (STL模拟)
问题描述 请你写一个命令行分析程序,用以分析给定的命 令行里包含哪些选项.每个命令行由若干个字符串组成,它们之间恰好由一个空格分隔.这些字符串中的第一个为该命令行工具的名字,由小写字母组成,你的程序 ...
- CCF 201403-2 窗口 (STL模拟)
问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示位于顶层的窗口里的 ...
- uva 327 Evaluating Simple C Expressions 简易C表达式计算 stl模拟
由于没有括号,只有+,-,++,--,优先级简单,所以处理起来很简单. 题目要求计算表达式的值以及涉及到的变量的值. 我这题使用stl的string进行实现,随便进行练手,用string的erase删 ...
- HDU5071 - Chat(STL模拟)
题目描述 略... 题解 现场赛的时候真是脑残...用splay去写..写完发现调试不出来...然后才发现数据范围才5000...不过那时候只有40分钟了..用数组模拟了速度敲了一发.写完只剩10几分 ...
- 牛客练习赛31 D 神器大师泰兹瑞与威穆 STL,模拟 A
牛客练习赛31 D 神器大师泰兹瑞与威穆 https://ac.nowcoder.com/acm/contest/218/D 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 26214 ...
随机推荐
- Firefox What's New 太难找了
Firefox每次更新版本,不知道更新了什么,找不到Waht‘s New,做个记录! 1.点击mozilla下拉 2.最左侧 MOZILLA>SUPPORT>Firefox 3.https ...
- Project2016下载安装密钥激活教程破解
project2016发布增加了许多功能.Microsoft Office 2016 官方正式版发布!这是微软发布的全新办公软件套件,相比现有Office 2013的变化也不是很大,界面和功能都只是微 ...
- mingw编译ffmpeg 错误:Unknown option "--enable-memalign-hack"
据说mingw编译ffmpeg的话需要添加 --enable-memalign-hack 开关 但如果源码是最新版比如:ffmpeg4.0.2 的话 好像已经禁用了该开关. “我可以确认新的ffmpe ...
- UI5-学习篇-13-Eclipse 开发UI5应用
1.Eclipse环境配置及组件安装 UI5-学习篇-1-Eclipse开发工具及环境搭建 2.创建项目 3.设置代理映射 打开WebContent->WEB-INF->web.xml文件 ...
- GCD 常用API 总结
dispatch_sync:同步操作,会阻塞当前线程 dispatch_async:普通的异步操作,也就是在指定的队列中添加一个block操作,不会阻塞当前线程 dispatch_group_asyn ...
- JDK1.8 LocalDateTime 时间类与字符互转
public static void main(String[] args) { DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPat ...
- 如何启用windows8, windows10中被停用的远程桌面,如何连接windows10远程桌面?
针对windows8.x中文版以及win10无远程桌面功能的解决办法: 第一步到一个网站上下载一个工具包. 这个是开源的.可以放心使用.下载地址: https://github.com/binarym ...
- LeetCode OJ 93. Restore IP Addresses
题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...
- cap文件的格式说明
前面24个字节是.cap文件的文件头. 头信息对应的结构体为:struct pcap_file_header { bpf_u_int32 magic; u_short version_major; ...
- DOM精简版笔记
1.1. 基本概念 1.1.1. DOM DOM Document Object Model 文档对象模型 就是把HTML文档模型化,当作对象来处理 DOM提供的一系列属性和方法可以 ...