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 ...
随机推荐
- Windows 端口占用解决
- Crontab 删除N天前日志
linux是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,让系统定时清理一些不需要的文件很有一种爽快的事情.不用你去每天惦记着是否需要清理日志 ...
- eclipse 代码检查插件使用
本文是按照以下这篇文章进行实践而来. 因此请参照: https://www.ibm.com/developerworks/cn/java/j-ap01117/index.html 五种插件: Ch ...
- UI5-学习篇-16-云端SCP-Destination配置
1.登录路径: https://account.ap1.hana.ondemand.com/#/home/welcome 2.查看云连接器 如下图所示,虚拟主机都已连接且资源可用: 若虚拟主机连接存在 ...
- Word Ladder 有必要深究。非图的广度优先遍历。标记
感觉很生疏. https://leetcode.com/problems/word-ladder/
- javascript:FileReader对象(读取文件)
FileReader对象 1.检测浏览器对FileReader的支持 1 if(window.FileReader) { 2 var fr = new FileReader(); 3 // add y ...
- 虚拟机centos NAT模式 配置静态ip
主要的设置有1.配置ip地址段,2.配置NAT(网关.ip地址端.子网掩码),3.修改网卡配置文件(/etc/sysconfig/network-scripts/ifcfg-eth0 ),4.重启网卡 ...
- 最邻近算法(KNN)识别数字验证码
应用场景 对于简单的数字型验证码的自动识别.前期已经完成的工作是通过切割将验证码图片切割成一个一个的单个数字的图片,并按照对应的数字表征类别进行分类(即哪些图片表示数字7,哪些表示8),将各种数字 ...
- editable : false与 readonly 的区别
editable : false 不能输入 readonly:不可操作,只能看
- IE 浏览器的兼容性列表设置
打开 IE 浏览器 IE8 为例如: