/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB

Code

#include <stdio.h>
#include <string.h>

int main() {
    int T;
    scanf("%d", &T);

    while (T--) {
        int N, k;
        scanf("%d %d", &N, &k);

        char num[105];
        scanf("%s", num);

        int keep = N - k;
        char result[105];  // To store the final number
        int top = -1;

        for (int i = 0; i < N; i++) {
            char current = num[i];

            // Pop smaller digits from the result if possible
            while (top >= 0 && result[top] < current && k > 0) {
                top--;
                k--;
            }

            result[++top] = current;  // Push current digit to result
        }

        result[keep] = '\0';  // Null-terminate only first (N - k) digits

        // Remove leading zeros (optional, problem doesn't mention)
        int start = 0;
        while (start < keep && result[start] == '0') start++;

        if (start == keep)
            printf("0\n");  // All zeros
        else
            printf("%s\n", result + start);
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1006 Remove K Digits
Language
C99 (GCC 13.2.0)
Submit At
2025-05-21 13:45:01
Judged At
2025-05-21 13:45:01
Judged By
Score
100
Total Time
1ms
Peak Memory
540.0 KiB