您的位置:首页 > 其它

[HDOJ1274]展开字符串

2015-09-04 02:54 399 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1274

递归题。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cctype>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <vector>

using namespace std;

char str[66666];
int len;
int solve(int pos) {
while(str[pos] != ')' && pos < len) {
int t = 0;
while(isdigit(str[pos])) {
t = t * 10 + str[pos++] - '0';
}
if(t == 0) {
t++;
}
int now = -1;
while(t--) {
if(str[pos] == '(') {
now = solve(pos+1);
}
else {
printf("%c", str[pos]);
}
}
if(now != -1) {
pos = now;
}
pos++;
}
return pos;
}

int main() {
// freopen("in", "r", stdin);
int T;
scanf("%d", &T);
getchar();
while(T--) {
memset(str, 0, sizeof(str));
gets(str);
len = strlen(str);
solve(0);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: