ZOJ2571 Big String Outspread 模拟

题意:给定一个字符串,按照要求输出来。

解法:每次进行一次首字母判定,然后根据不同的情况进行递归。

代码如下:

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

char str[300];

string display(int ti, int sta, int fuck) {
    string ret;
    if (sta > fuck) return ret;
    if (sta == fuck && !isalpha(str[sta])) return ret;
    string tmp;
    if (!isalpha(str[sta])) { // 如果不是以字符开始
        if (isdigit(str[sta])) {
            int t = 0, i, j;
            for (i = sta; isdigit(str[i]); ++i) { // 如果是数字的话 
                t = t * 10 + (str[i] - '0'); // 得出循环的次数数字
            }
            if (str[i] != '(') { // 如果后面不是括号,那么只有一位 
                tmp = display(t, i, i) + display(1, i+1, fuck);
            } else {
                int cnt = 0;
                for (j = sta; ; ++j) { // 找到括号在的位置 
                    if (str[j] == ')') {
                        --cnt;
                        if (!cnt) break;
                    }
                    else if (str[j] == '(') ++cnt;
                }
                tmp = display(t, i+1, j-1) + display(1, j+1, fuck);
            }
        }
        else { // 如果是括号进入 
            int cnt = 0, j;
            for (j = sta; ; ++j) {
                if (str[j] == ')') {
                    --cnt;
                    if (!cnt) break;    
                }
                else if (str[j] == '(') ++cnt;
            }
            tmp = display(1, sta, j-1) + display(1, j+1, fuck);
        }
        while (ti--) {
            ret += tmp;
        }
    } else {
        tmp = str[sta] + display(1, sta+1, fuck);
        while (ti--)
            ret += tmp; // 把后面的这一个字母进行重复
    }
    return ret;
}

int main() {
    int T, len;
    scanf("%d", &T);
    while (T--) {
        scanf("%s", str);
        len = strlen(str);
        cout << display(1, 0, len-1) << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Lyush/p/3023202.html