CSES - Datatähti 2022 alku - Results
Submission details
Task:Ositus
Sender:wolruso
Submission time:2021-10-18 00:45:46 +0300
Language:C++11
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3details
#2ACCEPTED0.01 s1, 2, 3details
#30.01 s1, 2, 3details
#40.01 s1, 2, 3details
#50.01 s2, 3details
#60.01 s3details
#70.01 s3details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", str);
     ~~~~~^~~~~~~~~~~

Code

#include <stdio.h>
#include <stdint.h>

uint64_t getNumCombinations(char *str, uint32_t size, uint32_t previousIndx) {
    if(previousIndx + 1 >= size) return 1;
    uint64_t count;
    uint32_t chrCount[26];
    uint8_t chrIndex;
    
    for(uint8_t i = 0; i < 26; i++) chrCount[i] = 0;
    count = 0;

    for(uint32_t i = previousIndx + 1; i < size; i++) {
        chrIndex = str[i - 1] - 'a';

        chrCount[chrIndex]++;
        if(chrCount[chrIndex] > 1) return count;

        count += getNumCombinations(str, size, i) % 1000000007;
        count %= 1000000007;
    }
    if(chrCount[str[size - 1] - 'a'] == 0) count++;
    return count % 1000000007;
}

#define MAX_CALLS 50000

uint64_t getNumCombinationsIterative(char *str, uint32_t strLength) {
    uint64_t count;
    uint8_t chrIndex;
    uint32_t chrCount[MAX_CALLS][26];
    uint32_t indxStack[MAX_CALLS];
    int64_t stackTop;
    
    // Initialize stacks
    stackTop = 0;
    for(uint8_t i = 0; i < 26; i++) chrCount[stackTop][i] = 0;
    indxStack[stackTop] = 1;
    
    count = 0;

    while(stackTop >= 0) {
        if(indxStack[stackTop] == strLength) {
            if(chrCount[stackTop][str[strLength - 1] - 'a'] == 0) count++;
            count %= 1000000007;
            stackTop--;
            continue;
        }
        // Increment chrCount[stackTop][chrIndex]
        chrIndex = str[indxStack[stackTop] - 1] - 'a';
        chrCount[stackTop][chrIndex]++;
        // Quit if same character occurred twice in this stackTop
        if(chrCount[stackTop][chrIndex] > 1) {
            stackTop--;
            break;
        }
        // Increment indxStack[indxStackTop] since next time should continue from next chr
        indxStack[stackTop]++;
        // If reached end of str, increment count
        if(indxStack[stackTop] == strLength) {
            count++;
            count %= 1000000007;
        }
        // Otherwise, call recursively
        else {
            stackTop++;
            for(uint8_t j = 0; j < 26; j++) chrCount[stackTop][j] = 0;
            indxStack[stackTop] = indxStack[stackTop - 1];
        }
    }
    return count;
}

int main() {
    char str[1000001];
    uint32_t strLength, i;
    uint64_t combinationCount;
    
    for(i = 0; i < 1000001; i++) str[i] = '\0';
    
    scanf("%s", str);
    for(i = 0; i < 1000000 && str[i] != '\0'; i++);
    strLength = i;
    //combinationCount = getNumCombinations(str, strLength, 0);
    combinationCount = getNumCombinationsIterative(str, strLength);
    printf("%lu", combinationCount);
    return 0;
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
a

correct output
1

user output
1

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
abcdefghij

correct output
512

user output
512

Test 3

Group: 1, 2, 3

Verdict:

input
abcabaacbc

correct output
120

user output
6

Test 4

Group: 1, 2, 3

Verdict:

input
aaxxxxxxaa

correct output
4

user output
2

Test 5

Group: 2, 3

Verdict:

input
mfyzvoxmppoxcvktmcjkryyocfweub...

correct output
643221148

user output
248

Test 6

Group: 3

Verdict:

input
weinscqmmpgbrlboocvtbptgbahmwv...

correct output
831644159

user output
448

Test 7

Group: 3

Verdict:

input
sxaoxcyrjoeieyinaqxwukgzdnhhsw...

correct output
816016015

user output
(empty)

Error:
*** stack smashing detected ***: <unknown> terminated