[HDOJ1274]展开字符串

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1274

 

递归题。

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cmath>
 7 #include <cctype>
 8 #include <queue>
 9 #include <map>
10 #include <set>
11 #include <stack>
12 #include <list>
13 #include <vector>
14 
15 using namespace std;
16 
17 
18 char str[66666];
19 int len;
20 int solve(int pos) {
21     while(str[pos] != ')' && pos < len) {
22         int t = 0;
23         while(isdigit(str[pos])) {
24             t = t * 10 + str[pos++] - '0';
25         }
26         if(t == 0) {
27             t++;
28         }
29         int now = -1;
30         while(t--) {
31             if(str[pos] == '(') {
32                 now = solve(pos+1);
33             }
34             else {
35                 printf("%c", str[pos]);
36             }
37         }
38         if(now != -1) {
39             pos = now;
40         }
41         pos++;
42     }
43     return pos;
44 }
45 
46 int main() {
47     // freopen("in", "r", stdin);
48     int T;
49     scanf("%d", &T);
50     getchar();
51     while(T--) {
52         memset(str, 0, sizeof(str));
53         gets(str);
54         len = strlen(str);
55         solve(0);
56         printf("\n");
57     }
58     return 0;
59 }

 

转载于:https://www.cnblogs.com/kirai/p/4781152.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值