Skip to content

Commit

Permalink
introduce subsequence
Browse files Browse the repository at this point in the history
  • Loading branch information
witek-formanski committed Oct 26, 2023
1 parent 3a14935 commit e2f1640
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
* [decompress.cpp](./src/decompress.cpp) (C++) :white_check_mark::microscope:
* [compress.cpp](./src/compress.cpp) (C++) :white_check_mark::microscope:
* [decompress.c](./src/decompress.c) (C)
* [zad. 9.] ile jest trójek spełniających nierówność trójkąta w tablicy
* [triangle_inequality](./src/triangle_inequality.c) (C)
* [zad. 10.] zabawka Jasia
* [cylinder_toy](./src/cylinder_toy.c) (C)
* [toy](./src/toy.c): autor: @lbozyk (C) :white_check_mark:
Expand All @@ -50,6 +52,8 @@
* [zad. 12.] następna permutacja
* [lexicographical_order](./src/lexicographical_order.c) (C)
* [next_perm](./src/next_perm.c): autor: @lbozyk (C) :white_check_mark:
* [zad. 13.] sprawdź, czy jeden ciąg zawiera się w drugim
* [subsequence](./src/subsequence.c) (C)
* [zad. 16.] ciąg róznicowy
* [differential_sequence](./src/differential_sequence.c) (C)
* [diff_seq](./src/diff_seq.c) (C): autor: @lbozyk (C) :white_check_mark:
Expand Down
70 changes: 70 additions & 0 deletions src/subsequence.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low;

for (int j = low; j <= high; j++) {
if (arr[j] < pivot) {
swap(arr[i], arr[j]);
i++;
}
}
swap(arr[i + 1], arr[high]);
return (i + 1);
}

void quick_sort(int arr[], int low, int high) {
if (low < high) {
int index = partition(arr, low, high);

quick_sort(arr, low, index - 1);
quick_sort(arr, index + 1, high);
}
}

// check if b[] contains all elements of a[]
bool is_subsequence(int a[], int size_a, int b[], int size_b) {
quick_sort(a, 0, size_a);
quick_sort(b, 0, size_b);

int j = 0;
for (int i = 0; i < size_a; i++) {
if (a[i] < b[i])
return false;
else if (a[i] > b[i])
i--;

j++;

if (j == size_b)
return false;
}

return true;
}

int main() {
int size_a, size_b;
if(!scanf("%d", &size_a)) printf("invalid value");
int* a = (int*)malloc((size_t)size_a * sizeof(int));
for (int i = 0; i < size_a; i++) {
if(!scanf("%d", &a[i])) printf("invalid value");
}

if(!scanf("%d", &size_b)) printf("invalid value");
int* b = (int*)malloc((size_t)size_b * sizeof(int));
for (int i = 0; i < size_b; i++) {
if(!scanf("%d", &b[i])) printf("invalid value");
}

printf(is_subsequence(a, size_a, b, size_b));
}
14 changes: 14 additions & 0 deletions src/triangle_inequality.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>
#include <stdlib.h>

// tab - all elements sorted in ascending order
// a < b < c
// c < a + b

int trojki(int size, int tab[]) {

}

int main() {

}
4 changes: 4 additions & 0 deletions tests/c/subsequence0.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
5
1 2 3 4 5
5
5 4 3 2 1
1 change: 1 addition & 0 deletions tests/c/subsequence0.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
4 changes: 4 additions & 0 deletions tests/c/subsequence1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
6
1 2 3 4 5 6
5
5 4 3 2 1
1 change: 1 addition & 0 deletions tests/c/subsequence1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
4 changes: 4 additions & 0 deletions tests/c/subsequence2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
6
1 2 3 4 5 6
7
-1 5 4 3 2 1 100
1 change: 1 addition & 0 deletions tests/c/subsequence2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0

0 comments on commit e2f1640

Please sign in to comment.