Submission details
Task:Tulkki
Sender:NicholasAhman
Submission time:2025-11-03 17:56:50 +0200
Language:C++ (C++17)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED12
#2ACCEPTED32
#3ACCEPTED56
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2, 3details
#2ACCEPTED0.00 s1, 2, 3details
#3ACCEPTED0.00 s1, 2, 3details
#4ACCEPTED0.00 s1, 2, 3details
#5ACCEPTED0.00 s1, 2, 3details
#6ACCEPTED0.00 s1, 2, 3details
#7ACCEPTED0.00 s2, 3details
#8ACCEPTED0.00 s2, 3details
#9ACCEPTED0.00 s2, 3details
#10ACCEPTED0.00 s2, 3details
#11ACCEPTED0.00 s2, 3details
#12ACCEPTED0.00 s2, 3details
#13ACCEPTED0.00 s3details
#14ACCEPTED0.00 s3details
#15ACCEPTED0.01 s3details
#16ACCEPTED0.00 s3details
#17ACCEPTED0.04 s3details
#18ACCEPTED0.01 s3details

Compiler report

input/code.cpp: In function 'void process_token(std::vector<Token>&, std::vector<Token>&, std::array<long unsigned int, 26>&, std::vector<Repeat>&, const Token&)':
input/code.cpp:57:18: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::array<long unsigned int, 26>::value_type' {aka 'long unsigned int'} [-Wformat=]
   57 |         printf("%d ", vars[token.data - 'A']);
      |                 ~^
      |                  |
      |                  int
      |                 %ld

Code

#include <array>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <vector>

enum class TokenType {
    CLEAR,
    INCREASE,
    PRINT,
    REPEAT,
};

struct Token {
    TokenType type;
    uint64_t data;
    size_t parent;
};

struct Repeat {
    size_t times;
    size_t start;
    size_t end;
};

int trim(char* str, int i) {
    while (true) {
        bool edited = false;
        while (isspace(str[i])) {
            i++;
            edited = true;
        }

        if (str[i] == '#') {
            while (str[i] != '\n') {
                i++;
                edited = true;
            }
        }

        if (!edited) {
            break;
        }
    }

    return i;
}

void process_token(std::vector<Token> &tokens, std::vector<Token> &repeat_tokens, std::array<uint64_t, 26> &vars, std::vector<Repeat> &repeats, const Token &token) {
    if (token.type == TokenType::CLEAR) {
        vars[token.data - 'A'] = 0;
    } else if (token.type == TokenType::INCREASE) {
        vars[token.data - 'A']++;
    } else if (token.type == TokenType::PRINT) {
        printf("%d ", vars[token.data - 'A']);
    } else if (token.type == TokenType::REPEAT) {
        const Repeat &repeat = repeats[token.data];
        const size_t repeat_times = vars[repeat.times - 'A'];

        for (size_t i = 0; i < repeat_times; i++) {
            for (size_t j = repeat.start; j < repeat.end; j++) {
                if (repeat_tokens[j].parent == token.data)
                    process_token(tokens, repeat_tokens, vars, repeats, repeat_tokens[j]);
            }
        }
    }
}

int main() {
    std::vector<Repeat> repeats;
    std::vector<Token> tokens;

    bool in_repeat = false;
    std::vector<Token> repeat_tokens;
    std::vector<size_t> repeat_stack;

    std::array<uint64_t, 26> vars = {0};

    char input[128*1000];
    char *ptr = input;
    size_t len = read(STDIN_FILENO, input, 128*1000);
    input[len] = '\0';
    while (true) {
        int i = 0;
        
        if (ptr[i] == '\0' or ptr[i] == EOF) {
            break;
        }

        i = trim(ptr, 0);

        Token token;
        token.data = 0;

        bool repeat_added = false;

        if (strncmp(ptr + i, "CLEAR", 5) == 0) {
            token.type = TokenType::CLEAR;
            i += 5;
        } else if (strncmp(ptr + i, "INCREASE", 8) == 0) {
            token.type = TokenType::INCREASE;
            i += 8;
        } else if (strncmp(ptr + i, "PRINT", 5) == 0) {
            token.type = TokenType::PRINT;
            i += 5;
        } else if (strncmp(ptr + i, "REPEAT", 6) == 0) {
            token.type = TokenType::REPEAT;
            i += 6;
            if (in_repeat) {
                repeats.push_back({0, repeat_tokens.size() + 1, 0});
            } else {
                repeats.push_back({0, repeat_tokens.size(), 0});
            }
            repeat_added = true;
        } else if (ptr[i] == '(') {
            ptr += i + 1;
            continue;
        } else if (ptr[i] == ')') {
            const size_t idx = repeat_stack.back();
            repeat_stack.pop_back();

            repeats[idx].end = repeat_tokens.size();
            ptr += i + 1;

            if (repeat_stack.empty()) {
                in_repeat = false;
            }
            continue;
        } else {
            break;
        }

        i = trim(ptr, i);

        token.data = ptr[i];

        int parent = -1;
        
        if (repeat_added) {
            i += 1;
            i = trim(ptr, i);
            if (strncmp(ptr + i, "TIMES", 5) != 0) {
                printf("Error: REPEAT must be followed by TIMES\n");
            }

            i += 5;
            i = trim(ptr, i);

            if (ptr[i] != '(') {
                printf("Error: TIMES must be followed by parentheses\n");
            }

            repeats.back().times = token.data;
            token.data = repeats.size() - 1;

            if (!repeat_stack.empty()) {
                parent = repeat_stack.back();
            }
            repeat_stack.push_back(repeats.size() - 1);
        }

        if (in_repeat) {
            if (parent == -1) {
                token.parent = repeat_stack.back();
            } else {
                token.parent = parent;
            }

            repeat_tokens.push_back(token);
        } else {
            tokens.push_back(token);

            if (repeat_added) {
                in_repeat = true;
            }
        }
        ptr += i + 1;
    }


    for (auto& token : tokens) {
        process_token(tokens, repeat_tokens, vars, repeats, token);
    }
}

Test details

Test 1 (public)

Group: 1, 2, 3

Verdict: ACCEPTED

input
PRINT X
INCREASE X
PRINT X
INCREASE X
PRINT X
...

correct output
0 1 2 0 

user output
0 1 2 0 

Test 2 (public)

Group: 1, 2, 3

Verdict: ACCEPTED

input
INCREASE
X
# aybabtu
   PRINT    X
INCREASE # test
...

correct output
1 3 

user output
1 3 

Test 3 (public)

Group: 1, 2, 3

Verdict: ACCEPTED

input
# Create number 3
INCREASE X
INCREASE X
INCREASE X

...

correct output

user output

Test 4 (public)

Group: 1, 2, 3

Verdict: ACCEPTED

input
INCREASE A
PRINT A
INCREASE B
PRINT B
INCREASE C
...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

Test 5 (public)

Group: 1, 2, 3

Verdict: ACCEPTED

input
INCREASE X
INCREASE X
INCREASE X
INCREASE X
INCREASE X
...

correct output
999 

user output
999 

Test 6 (public)

Group: 1, 2, 3

Verdict: ACCEPTED

input
PRINT X
PRINT X
PRINT X
PRINT X
PRINT X
...

correct output
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

user output
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

Test 7 (public)

Group: 2, 3

Verdict: ACCEPTED

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
5 5 5 5 5 

user output
5 5 5 5 5 

Test 8 (public)

Group: 2, 3

Verdict: ACCEPTED

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
0 0 0 0 0 

user output
0 0 0 0 0 

Test 9 (public)

Group: 2, 3

Verdict: ACCEPTED

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
6 7 8 9 10 

user output
6 7 8 9 10 

Test 10 (public)

Group: 2, 3

Verdict: ACCEPTED

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
5 5 

user output
5 5 

Test 11 (public)

Group: 2, 3

Verdict: ACCEPTED

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
20 

user output
20 

Test 12 (public)

Group: 2, 3

Verdict: ACCEPTED

input
INCREASE A
INCREASE A

INCREASE B
INCREASE B
...

correct output
42 

user output
42 

Test 13 (public)

Group: 3

Verdict: ACCEPTED

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 

user output
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 

Test 14 (public)

Group: 3

Verdict: ACCEPTED

input
# Create number 3
INCREASE A INCREASE A INCREASE...

correct output
12 

user output
12 

Test 15 (public)

Group: 3

Verdict: ACCEPTED

input
INCREASE X
INCREASE X
INCREASE X
INCREASE X
INCREASE X
...

correct output
531441 

user output
531441 

Test 16 (public)

Group: 3

Verdict: ACCEPTED

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
1337 

user output
1337 

Test 17 (public)

Group: 3

Verdict: ACCEPTED

input
INCREASE A
INCREASE A

REPEAT A TIMES (
    REPEAT A TIMES (
...

correct output
1 2 1 2 1 1 3 4 3 4 3 4 3 4 3 ...

user output
1 2 1 2 1 1 3 4 3 4 3 4 3 4 3 ...

Test 18 (public)

Group: 3

Verdict: ACCEPTED

input
# Efficient algorithm for find...

correct output
2 3 5 7 11 13 17 19 23 29 31 3...

user output
2 3 5 7 11 13 17 19 23 29 31 3...