记录编号 49638 评测结果 AAAAAAAAAA
题目名称 [NOIP 2010冲刺十三]外星密码 最终得分 100
用户昵称 Gravatarfanzeyi 是否通过 通过
代码语言 C++ 运行时间 0.007 s
提交时间 2012-11-08 18:58:34 内存使用 1.96 MiB
显示代码纯文本
#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; 
}