diff --git a/README.md b/README.md index 59b4b10..fa8d7d1 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: diff --git a/src/subsequence.c b/src/subsequence.c new file mode 100644 index 0000000..1202b5a --- /dev/null +++ b/src/subsequence.c @@ -0,0 +1,70 @@ +#include +#include +#include + +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)); +} \ No newline at end of file diff --git a/src/triangle_inequality.c b/src/triangle_inequality.c new file mode 100644 index 0000000..da272e7 --- /dev/null +++ b/src/triangle_inequality.c @@ -0,0 +1,14 @@ +#include +#include + +// tab - all elements sorted in ascending order +// a < b < c +// c < a + b + +int trojki(int size, int tab[]) { + +} + +int main() { + +} \ No newline at end of file diff --git a/tests/c/subsequence0.in b/tests/c/subsequence0.in new file mode 100644 index 0000000..8fe67d3 --- /dev/null +++ b/tests/c/subsequence0.in @@ -0,0 +1,4 @@ +5 +1 2 3 4 5 +5 +5 4 3 2 1 \ No newline at end of file diff --git a/tests/c/subsequence0.out b/tests/c/subsequence0.out new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/tests/c/subsequence0.out @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/tests/c/subsequence1.in b/tests/c/subsequence1.in new file mode 100644 index 0000000..7e49b8e --- /dev/null +++ b/tests/c/subsequence1.in @@ -0,0 +1,4 @@ +6 +1 2 3 4 5 6 +5 +5 4 3 2 1 \ No newline at end of file diff --git a/tests/c/subsequence1.out b/tests/c/subsequence1.out new file mode 100644 index 0000000..c227083 --- /dev/null +++ b/tests/c/subsequence1.out @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/tests/c/subsequence2.in b/tests/c/subsequence2.in new file mode 100644 index 0000000..3afa2c1 --- /dev/null +++ b/tests/c/subsequence2.in @@ -0,0 +1,4 @@ +6 +1 2 3 4 5 6 +7 +-1 5 4 3 2 1 100 \ No newline at end of file diff --git a/tests/c/subsequence2.out b/tests/c/subsequence2.out new file mode 100644 index 0000000..c227083 --- /dev/null +++ b/tests/c/subsequence2.out @@ -0,0 +1 @@ +0 \ No newline at end of file