比赛 20121030 评测结果
题目名称 外星密码 最终得分 0
用户昵称 fanzeyi 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2012-11-08 18:57:46
显示代码纯文本
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define MAX_LEN 20001

bool isNum(char c) {
    if(c <= '9' && c >= '0') {
        return true; 
    }
    return false; 
}

char *expand(char *in) {
    char *re; 
    char *result = (char*)calloc(MAX_LEN, sizeof(char)); 
    int len = strlen(in); 
    int p = 0; 
    int repeat = 0;
    int stack = 0; 
    bool meet_alpha = false; 
    while(p < len) {
        if(isNum(in[p]) && meet_alpha == false) {
            repeat = repeat * 10 + (in[p] - '0'); 
            p = p + 1; 
        }else if(in[p] == '[') {
            meet_alpha = true; 
            p = p + 1; 
            re = expand(&in[p]); 
            strcpy(&result[strlen(result)], re); 
            for(int i = p; i < len; i++) {
                if(in[i] == ']') {
                    if(stack == 0) {
                        p = i + 1; 
                        break; 
                    }
                    stack = stack - 1; 
                }else if(in[i] == '[') {
                    stack = stack + 1; 
                }
            }
        }else if(in[p] == ']') {
            meet_alpha = true; 
            re = (char*)calloc(strlen(result), sizeof(char)); 
            strcpy(re, result); 
            if(repeat) {
                for(int i = 1; i < repeat; i++) {
                    strcpy(&result[strlen(result)], re); 
                }
            }
            free(re); 
            return result; 
        }else{
            meet_alpha = true; 
            result[strlen(result)] = in[p++]; 
        }
    }
    return result; 
}

int main() {
    FILE *fin = fopen("passworda.in", "r"); 
    FILE *fout = fopen("passworda.out", "w"); 
    char strin[MAX_LEN]; 
    fscanf(fin, "%s", strin); 
    fprintf(fout, "%s", expand(strin)); 
    return 0; 
}