[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("
");
57     }
58     return 0;
59 }
原文地址:https://www.cnblogs.com/kirai/p/4781152.html