Moved kattis from Uni repo
This commit is contained in:
1
kattis/problems/01_11_23/keystrokes/1.in
Normal file
1
kattis/problems/01_11_23/keystrokes/1.in
Normal file
@ -0,0 +1 @@
|
||||
iLnLnLeLb
|
1
kattis/problems/01_11_23/keystrokes/2.in
Normal file
1
kattis/problems/01_11_23/keystrokes/2.in
Normal file
@ -0,0 +1 @@
|
||||
arnarLLLBBun
|
1
kattis/problems/01_11_23/keystrokes/3.in
Normal file
1
kattis/problems/01_11_23/keystrokes/3.in
Normal file
@ -0,0 +1 @@
|
||||
password123
|
60
kattis/problems/01_11_23/keystrokes/main.c
Normal file
60
kattis/problems/01_11_23/keystrokes/main.c
Normal file
@ -0,0 +1,60 @@
|
||||
#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;
|
||||
}
|
58
kattis/problems/01_11_23/keystrokes/main.c.orig
Normal file
58
kattis/problems/01_11_23/keystrokes/main.c.orig
Normal file
@ -0,0 +1,58 @@
|
||||
#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 = 0;
|
||||
int j = 0;
|
||||
while (i < len) {
|
||||
// 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);
|
||||
char corrected[MAX];
|
||||
|
||||
int newLen = transform(s, len, corrected);
|
||||
printf("%d\n", newLen);
|
||||
printStr(corrected, newLen);
|
||||
return 0;
|
||||
}
|
22
kattis/problems/01_11_23/keystrokes/main.py
Normal file
22
kattis/problems/01_11_23/keystrokes/main.py
Normal file
@ -0,0 +1,22 @@
|
||||
string = input()
|
||||
string_list = []
|
||||
cursor = 0
|
||||
for char in string:
|
||||
# If 'L' found in string, we move the cursor to the left
|
||||
if char == 'L':
|
||||
if cursor > 0:
|
||||
cursor -= 1
|
||||
# If 'R' found in string, we move the cursor to the right
|
||||
elif char == 'R':
|
||||
if cursor < len(string):
|
||||
cursor += 1
|
||||
# If 'B' found in string, we delete the character to the left of the cursor
|
||||
elif char == 'B':
|
||||
del string_list[cursor - 1]
|
||||
cursor -= 1
|
||||
# If any other character found in string, we insert it to the left of the cursor
|
||||
else:
|
||||
string_list.insert(cursor, char)
|
||||
cursor += 1
|
||||
# Print only the new string, without the initial one
|
||||
print(''.join(string_list))
|
BIN
kattis/problems/01_11_23/keystrokes/program
Executable file
BIN
kattis/problems/01_11_23/keystrokes/program
Executable file
Binary file not shown.
Reference in New Issue
Block a user