Submission #55822676


Source Code Expand

import itertools

def main():
    n, k = map(int, input().split())
    s = input()

    a = [ord(ch) - ord('a') for ch in s]
    a.sort()
    ans = 0

    while True:
        ok = True
        for i in range(n - k + 1):
            flag = True
            for j in range(k):
                if a[i + j] != a[i + k - 1 - j]:
                    flag = False
                    break
            if flag:
                ok = False
                break
        if ok:
            ans += 1
        if not next_permutation(a):
            break

    print(ans)

def next_permutation(seq):
    """
    Transform list into the next permutation.
    Return False if there is no next permutation.
    """
    # Find non-increasing suffix
    i = len(seq) - 1
    while i > 0 and seq[i - 1] >= seq[i]:
        i -= 1
    if i <= 0:
        return False

    # Find successor to pivot
    j = len(seq) - 1
    while seq[j] <= seq[i - 1]:
        j -= 1
    seq[i - 1], seq[j] = seq[j], seq[i - 1]

    # Reverse suffix
    seq[i:] = seq[i:][::-1]
    return True

if __name__ == "__main__":
    main()

Submission Info

Submission Time
Task C - Avoid K Palindrome 2
User Fuyumikan
Language Python (PyPy 3.10-v7.3.12)
Score 300
Code Size 1146 Byte
Status AC
Exec Time 294 ms
Memory 83296 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 38
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt
Case Name Status Exec Time Memory
example_00.txt AC 53 ms 76656 KiB
example_01.txt AC 53 ms 76376 KiB
example_02.txt AC 108 ms 83188 KiB
hand_00.txt AC 54 ms 76376 KiB
hand_01.txt AC 53 ms 76528 KiB
hand_02.txt AC 294 ms 82840 KiB
hand_03.txt AC 258 ms 82856 KiB
hand_04.txt AC 53 ms 76676 KiB
hand_05.txt AC 90 ms 83296 KiB
hand_06.txt AC 92 ms 83172 KiB
random_00.txt AC 54 ms 76512 KiB
random_01.txt AC 53 ms 76900 KiB
random_02.txt AC 54 ms 76708 KiB
random_03.txt AC 54 ms 76440 KiB
random_04.txt AC 54 ms 76452 KiB
random_05.txt AC 55 ms 80492 KiB
random_06.txt AC 70 ms 81668 KiB
random_07.txt AC 65 ms 81848 KiB
random_08.txt AC 68 ms 81828 KiB
random_09.txt AC 74 ms 82436 KiB
random_10.txt AC 67 ms 81912 KiB
random_11.txt AC 106 ms 82832 KiB
random_12.txt AC 78 ms 83192 KiB
random_13.txt AC 79 ms 82888 KiB
random_14.txt AC 179 ms 83176 KiB
random_15.txt AC 89 ms 82964 KiB
random_16.txt AC 106 ms 82916 KiB
random_17.txt AC 162 ms 83136 KiB
random_18.txt AC 54 ms 76492 KiB
random_19.txt AC 53 ms 76796 KiB
random_20.txt AC 54 ms 76560 KiB
random_21.txt AC 53 ms 76500 KiB
random_22.txt AC 67 ms 81728 KiB
random_23.txt AC 73 ms 82232 KiB
random_24.txt AC 71 ms 81904 KiB
random_25.txt AC 71 ms 81952 KiB
random_26.txt AC 82 ms 82660 KiB
random_27.txt AC 84 ms 82712 KiB