Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercises from list 6, 7 & partially 8 #24

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/cw6/zad12/slowa.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#define STOP_SYNTAX 'c'

#include <iostream>
#include <queue>

using namespace std;

int slowa(string s)
{
queue<char> q, k;
// fill q with default data
q.push('a');
q.push(STOP_SYNTAX);
q.push('b');
q.push(STOP_SYNTAX);
int count = 1;
bool wasB = false;

for (int i = 0; s[i]; i++)
{
if (s[i] == 'b' && !wasB)
wasB = true;

// do we encounter next fib letter
if (s[i] == q.front())
{
k.push(s[i]);
q.pop();

// if it was not last letter of word
if (q.front() == STOP_SYNTAX)
{
++count;
k.push(STOP_SYNTAX);
// copy finished word to make queue for next one
while (k.front() != STOP_SYNTAX)
{
q.push(k.front());
k.push(k.front());
k.pop();
}
}
}
}

// free q,k but i cannot do it
if (wasB)
return count;
return 0;
}

int main()
{
string slowo = "abccxabbbaaxccbbba" + 0;
cout << slowa(slowo) << endl;
}
93 changes: 93 additions & 0 deletions src/cw6/zad3/zad3_ms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <iostream>
#include <stack>

using namespace std;

bool is_ice(int layer)
{
return layer < 0;
}

void stack_layer(stack<int> *layers, int layer, bool isIce)
{
if (isIce)
{
layers->push(-abs(layer));
}
else
{
if (!layers->empty())
layers->push(abs(layer));
}
}

void put_on_stack(stack<int> *layers, int layer)
{
if (layers->empty())
{
stack_layer(layers, layer, is_ice(layer));
return;
}

int sameType = 0, otherType = 0;
while (otherType < abs(layer) && !layers->empty())
{
int topLayer = layers->top();
if (is_ice(layer) != is_ice(topLayer))
{
otherType += abs(topLayer);
}
else
{
sameType += topLayer;
}
layers->pop();
}

if (otherType < abs(layer))
{
put_on_stack(layers, layer);
return;
}

int remainingLayerOfOtherType = otherType - abs(layer);
stack_layer(layers, remainingLayerOfOtherType, !is_ice(layer));
stack_layer(layers, sameType+layer, is_ice(layer));
}

int sadzawka(int size, int arr[])
{
stack<int> layers;
for (int i = 0; i < size; i++)
{
put_on_stack(&layers, arr[i]);
}

// copy results
int resultsCount = (int)layers.size();
for (int i = 0; i < resultsCount; i++)
{
arr[i] = layers.top();
layers.pop();
}
return resultsCount;
}

int main()
{
int size;
cin >> size;
int *arr = (int *)malloc((size_t)size * sizeof(int));
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}

int results = sadzawka(size, arr);
for (int i = 0; i < results; i++)
{
cout << arr[i] << endl;
}

free(arr);
}
62 changes: 62 additions & 0 deletions src/cw6/zad5/wtc_ms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#define NOTHING_HERE -1

#include <iostream>
#include <stack>

using namespace std;

/*
5 #
4 # #
3 ####
2 ######
1 #######
i 1234567
k 1214111
*/

/// @brief
/// @param arr array with towers [1..n]
/// @param size elements count n
/// @param results array [1..n] with k's, when arr[i] = max(arr[i-results[i]+1], ... arr[i])
/// @return
int *katastrofa(int *arr, int size)
{
stack<int> indexes;
// if all towers are lower than current, when
indexes.push(NOTHING_HERE);

int *results = (int *)malloc((size_t)(size + 1) * sizeof(int));
for (int i = 1; i <= size; i++)
{
// remove all lower / same towers, because there are irrelevants
while (indexes.top() != NOTHING_HERE && arr[indexes.top()] <= arr[i])
indexes.pop();
// because indexes.top() is the index of first higher than arr[i] tower
// if arr[i] is the highest yet then
if (indexes.top() == NOTHING_HERE)
results[i] = i;
else
results[i] = i - indexes.top();
// add current tower
indexes.push(i);
}
return results;
}

int main()
{
int n;
cin>>n;
int* arr = (int*)malloc((size_t)(n+1)*sizeof(int));
for(int i = 1; i<=n; i++){
cin>>arr[i];
}
int* results = katastrofa(arr,n);
for(int i = 1; i<=n; i++){
cout<<results[i]<<endl;
}

free(arr);
free(results);
}
71 changes: 71 additions & 0 deletions src/cw6/zad7/manhatan_ms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <iostream>
#include <stack>

using namespace std;

int best_area(stack<int> *towers, int arr[], int currentTower, int i)
{
int max = 0;
while (!towers->empty() && arr[towers->top()] > currentTower)
{
int width = (i-1) - towers->top() + 1;
int area = width * arr[towers->top()];
if (area > max)
{
max = area;
}
towers->pop();
}
return max;
}

int prostokat(int n, int arr[])
{
if (n <= 0)
return 0;
stack<int> towers;
towers.push(0);
int max = 0;
for (int i = 1; i < n; i++)
{
int currArea = best_area(&towers, arr, arr[i], i);
if (currArea > max)
max = currArea;
towers.push(i);
}
return max;
}

int prostokat_brut(int n, int arr[])
{
int max = 0;
for (int i = 0; i < n; i++)
{
int currMin = arr[i];
for (int j = i; j < n; j++)
{
if (currMin > arr[j])
currMin = arr[j];

int currArea = (j-i+1) * currMin;
if(currArea > max)
max=currArea;
}
}
return max;
}

int main()
{
int n;
cin >> n;
int *arr = (int *)malloc((size_t)n * sizeof(int));
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int bestArea = prostokat(n, arr);
cout << bestArea << endl;

free(arr);
}
94 changes: 94 additions & 0 deletions src/cw6/zad9/zad9_ms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <iostream>
#include <stack>

// let: results fit in arr
void inc(int n, bool arr[])
{
if (n <= 0)
return;

int j;
if (arr[0])
{
arr[0] = false;
arr[1] = true;

// double 1 can be at arr[1] arr[2]
j = 2;
}
else{
arr[0] = true;
// double 1 can be at arr[0] arr[1]
j = 1;

}

while (j < n && arr[j - 1] && arr[j])
{
// if there are two 1 side by side, then represent them as 1 on arr[j+1]
arr[j + 1] = true;
arr[j - 1] = false;
arr[j] = false;

j+=2;
}
}

int fib(int n)
{
if (n == 0)
return 1;
if (n == 2)
return 2;
return fib(n - 2) + fib(n - 1);
}

// int to_dec_from_zeckendorf(int n, bool arr[])
// {
// int decValue = 0;
// for (int i = 0; i < n; i++)
// {
// if (arr[i])
// decValue += fib(i);
// }
// return decValue;
// }

int scan_bool(bool *results)
{
int input;
int response = scanf("%d", &input);
if (response)
{
*results = (bool)input;
}
return response;
}

int main()
{
int n;
if (!scanf("%d", &n))
{
printf("wrong n");
return -1;
}

bool *arr = (bool *)malloc((size_t)n * sizeof(bool));
for (int i = 0; i < n; i++)
{
if (!scan_bool(&arr[i]))
{
printf("wrong bool");
i--;
}
}

inc(n, arr);
for (int i = 0; i < n; i++)
{
printf("%d\n", (int)arr[i]);
}

free(arr);
}
Loading
Loading