2023-12-01 13:35:13 +01:00

60 lines
1.2 KiB
C

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000000
int transform(char *src, int len, char *dst) {
// Traverse the string
int i = len;
int j = 0;
while (i > 0) {
// printf("%c\n", src[i]);
// If we encounter a backspace, we need to remove the last character
// from the destination string
if (src[i] == 'B') {
if (j > 0) {
j--;
}
} else if (src[i] == 'L') {
// If we encounter a left button, we have to move back once
if (j > 0) {
j--;
}
} else if (src[i] == 'R') {
// If we encounter a right button, we have to move forward once
if (j < len) {
j++;
}
}
else {
// Otherwise, we add the character to the destination string
dst[j] = src[i];
j++;
}
i--;
}
return j;
}
void printStr(char *s, int len) {
for (int i = 0; i < len; ++i) {
printf("%c", s[i]);
}
printf("\n");
}
int main(int argc, char **argv) {
char s[MAX];
scanf("%s", s);
int len = strlen(s);
printf("%d\n", len);
char corrected[MAX];
int newLen = transform(s, len, corrected);
printf("%d\n", newLen);
printStr(corrected, newLen);
return 0;
}