【HDOJ】1274 展开字符串
栈的应用,需要注意括号前可能没有数字的情况。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <string>
using namespace std; #define MAXN 300
#define isdigit(c) (c>='0' && c<='9')
#define islower(c) (c>='a' && c<='z')
char str[MAXN]; void solve() {
string ans = "";
stack<char> ops;
stack<int> cnts;
stack<string> strs;
int cnt;
int i = ; while (str[i]) {
bool found = false;
cnt = ;
while (isdigit(str[i])) {
cnt = *cnt + str[i]-'';
++i;
found = true;
}
if (str[i] == '(') {
if (!found)
cnt = ;
ops.push('(');
cnts.push(cnt);
strs.push(ans);
ans = "";
} else if (str[i] == ')') {
cnt = cnts.top();
cnts.pop();
ops.pop();
int r = cnt;
string bk = ans;
ans = strs.top();
strs.pop();
while (r) {
if (r & )
ans += bk;
bk += bk;
r >>= ;
}
} else {
if (found) {
string bk(cnt, str[i]);
ans += bk;
} else {
ans += str[i];
}
}
++i;
} cout <<ans<<endl;
} int main() {
int t;
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
scanf("%d", &t);
while (t--) {
scanf("%s", str);
solve();
} return ;
}
【HDOJ】1274 展开字符串的更多相关文章
- HDU 1274 展开字符串 (递归+string类)
题目链接:HDU 1274 展开字符串 中文题. 左括号进入DFS函数,右括号return到上一层. 注意return回去的是这个一层递归中的括号中的字母串. AC代码: #include<st ...
- (DFS)展开字符串 -- hdu -- 1274
http://acm.hdu.edu.cn/showproblem.php?pid=1274 展开字符串 Time Limit: 2000/1000 MS (Java/Others) Memor ...
- 展开字符串(dfs)
展开字符串 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 1267: 展开字符串(Java)
WUSTOJ 1267: 展开字符串 参考 jamesMusk的博客--Java 判断字符是大写小写或者数字 Description 给三个参数a1,a2,a3和一个字符串,请按以下要求展开该字符 ...
- Luogu 1098 - 字符串的展开 - [字符串操作][模拟]
题目链接:https://www.luogu.org/problemnew/show/P1098 题目描述在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中 ...
- HDU-1274 展开字符串
Problem Description 在纺织CAD系统开发过程中,经常会遇到纱线排列的问题.该问题的描述是这样的:常用纱线的品种一般不会超过25种,所以分别可以用小写字母表示不同的纱线,例如:abc ...
- python全栈开发-Day3 字符串
python全栈开发-Day3 字符串 一.按照以下几个点展开字符串的学习 #一:基本使用 1. 用途 #首先字符串主要作用途径:名字,性别,国籍,地址等描述信息2.定义方式 在单引号\双引号\三引 ...
- string黑科技
1. string对象的定义和初始化以及读写 string s1; 默认构造函数,s1为空串string s2(s1); 将s2初始化为s1的一个副本string s3("valuee&qu ...
- 2178 表达式运算Cuties
2178 表达式运算Cuties 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 大师 Master 题解 题目描述 Description 给出一个表达式,其中运算 ...
随机推荐
- poj 3154 Graveyard 贪心
//poj 3154 //sep9 #include <iostream> #include <cmath> using namespace std; double a[204 ...
- GiB与GB
Gibibyte(giga binary byte的缩写)是信息或计算机硬盘存储的一个单位,简称GiB.由来“GiB”.“KiB”.“MiB”等是于1999年由国际电工协会(IEC)拟定了" ...
- POJ2250:Compromise(LCS)
Description In a few months the European Currency Union will become a reality. However, to join the ...
- 浅谈Android架构之MVP,MVVM
概述 MVP(Model-View-Presenter)是传统MVC(Model-View-Controller)在Android开发上的一种变种.进化模式.主要用来隔离UI.UI逻辑和业务逻辑.数据 ...
- redis 多实例配置
(redis的安装, 配置, 登陆等基础不再多说, 网上很多资料的, 这里只说个人对redis多实例的理解与配置) 我自己使用的redis版本是 2.8.13, 环境是 ubuntu 个人对多实例的理 ...
- Command 命令模式
简介 将来自客户端的请求传入一个对象,从而使你可用不同的请求对客户进行参数化.用于[行为请求者]与[行为实现者]解耦,可实现二者之间的松耦合,以便适应变化. 将一个请求封装为一个对象,从而使你可用不同 ...
- MSSQLSERVER服务不能启动
自从用上mysql,好久没打开sqlserver了,今天本想打开调试下MFC连接sqlserver,然后意外发现不能登录,之后我以为是sql服务没启动,然后去启动,还是没用,并且MSSQLSERVER ...
- 2015-09-22CSS:border、background、表格、超链接、overflow、firebug
1.CSS的border属性 ⑴定义和用法 border 简写属性在一个声明设置所有的边框属性. 可以按顺序设置如下属性: border-width border-style border-color ...
- HTML5 类型数组TypeArray(一)
1.起源 TypedArray是一种通用的固定长度缓冲区类型,允许读取缓冲区中的二进制数据. 其在WEBGL规范中被引入用于解决Javascript处理二进制数据的问题. TypedArray已经被大 ...
- adb shell - device not found
如果是真机,则连接usb即可(我的是真机).