Skip to content

Commit

Permalink
add various drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
witek-formanski committed Nov 20, 2023
1 parent a21575f commit 7820c1e
Show file tree
Hide file tree
Showing 6 changed files with 460 additions and 6 deletions.
53 changes: 53 additions & 0 deletions src/cw6/zad12/fibonacci_word.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// pomysł: wystarczy szukać zachłannie od lewej
// dwie kolejki:
// na jednej spamiętujemy, którego słowa szukamy
// na drugiej pamiętamy, które słowo dokleimy

// 'c' - znak stop

#include <queue>

int fib_word(char s[]) {
std::queue<char> q; // compare
std::queue<char> k; // build
int count = 1;
q.push('a');
q.push('c');
q.push('b');
q.push('c');

for (int i = 0; s[i]; ++i) {
if (s[i] == q.front()) {
k.push(s[i]);
q.pop();
if (q.front() == 'c') {
++count;
k.push('c');
while (k.front() != 'c') {
q.push(k.front());
k.push(k.front());
k.pop();
q.push('c');
k.pop(); // remove 'c'
}
}
}
}

if (count == 1)
for (int i = 0; s[i]; ++i) {
if (s[i] == 'b')
return 1;

return 0;
}

return count;
}

#include <iostream>

int main() {
char my_string[20] = "abccxabbbaaxccbbba";
std::cout << fib_word(my_string) << "\n";
}
41 changes: 41 additions & 0 deletions src/cw6/zad2/lifo_stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stack>

class lifo {
std::stack<int> up;
std::stack<int> down;

private:
void move(std::stack<int> down, std::stack<int> up) {
while (!down.empty()) {
up.push(down.top());
down.pop();
}
}

public:
int front() {
if (up.empty())
move(down, up);

return up.top();
}

void push(int k) {
down.push(k);
}

void pop() {
if (!up.empty())
move(down, up);

up.pop();
}
};

/*
kolejka dwustronna od 1 do k
n zapytań
O(n*k) jeśli nie optymalizujemy
O(nlogk) jeśli trzymamy równej wysokości stosy
(wyrównujemy gdy jeden jest pusty)
*/
18 changes: 12 additions & 6 deletions src/cw6/zad3/pond.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <cmath>
#include <iostream>
#include <stack>
#include <cmath>

void freeze(std::stack<int> s, int degrees) {
// int top = s.top();
Expand All @@ -25,11 +25,17 @@ void defrost(std::stack<int> s, int degrees) {
while (!s.empty() && degrees != 0) {
top = s.top();
s.pop();
if (std::abs(top + degrees) < std::abs(top)) {
new_next = top + degrees;
new_top = degrees;
s.push(new_next);
s.push(new_top);
if (degrees * top < 0) { // different signs
if (std::abs(degrees) < std::abs(top)) {
new_next = top + degrees;
new_top = degrees;
s.push(new_next);
s.push(new_top);
} else {
degrees += top;
}
} else { // the same signs

}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/other/count_thue_morse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
chcemy sprawdzić, ile jest podsłów
słowa Thuego-Morse'a długości length
01100110
*/

int count_thue_morse_words(int length) {

}
156 changes: 156 additions & 0 deletions src/other/thue_morse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
0 -> 01
1 -> 10
0
01
0110
01101001
0110100110010110
https://math.stackexchange.com/questions/4342938/subwords-of-the-thue-morse-sequence#:~:text=The%20Thue%2DMorse%20sequence%20can,0%20and%2010%20by%201.
101000011010
10 10 00 01 10 10
01 01 00 00 11 01 01
*/

unsigned int count_bits(int word) {
int number_of_bits = 0;
while (word) {
word /= 2;
++number_of_bits;
}

return number_of_bits;
}

bool can_be_decreased(int word, unsigned int number_of_bits) {
int last_two_bits;
while (number_of_bits > 1) {
last_two_bits = word & 3;
if (last_two_bits == 3 || last_two_bits == 0)
return false;
word /= 4;
number_of_bits -= 2;
}

return true;
}

unsigned int mirror_bits(unsigned int word, int number_of_bits) {
unsigned int mirrored_word = 0;
while (number_of_bits > 0) {
mirrored_word += word & 1;
--number_of_bits;
}

return mirrored_word;
}

unsigned int undo_thue_morse_case_one(unsigned int word, int number_of_bits) {
int last_two_bits;
unsigned int new_word;
int new_bits = 0;
while (number_of_bits > 1) {
last_two_bits = word & 3;
new_word *= 2;
++new_bits;
if (last_two_bits == 2) { // 10 -> 1
new_word += 1;
}
// else // if (last_two_bits == 1) // 01 -> 0
word /= 4;
number_of_bits -= 2;
}

if (number_of_bits == 1) {
new_word *= 2;
++new_bits;
if (word & 1 == 0) { // 10...
new_word += 1;
}
}

return mirror_bits(new_word, number_of_bits);
}

unsigned int undo_thue_morse_case_two(unsigned int word, int number_of_bits) {
int last_two_bits;
unsigned int new_word;
int new_bits = 0;

new_word *= 2;
++new_bits;
if (word & 1 == 1) { // ...10
new_word += 1;
}

while (number_of_bits > 1) {
last_two_bits = word & 3;
new_word *= 2;
++new_bits;
if (last_two_bits == 2) { // 10 -> 1
new_word += 1;
}
// else // if (last_two_bits == 1) // 01 -> 0
word /= 4;
number_of_bits -= 2;
}

if (number_of_bits == 1) {
new_word *= 2;
++new_bits;
if (word & 1 == 0) { // 10...
new_word += 1;
}
}

return mirror_bits(new_word, number_of_bits);
}

bool is_thue_morse_subword(int word) {
int last_two_bits;
int new_word = 0;
unsigned int number_of_bits = count_bits(word);
while (number_of_bits > 2) {
bool case_one = can_be_decreased(word, number_of_bits); // case 1: ... 2 2 2
if (case_one) {
word = undo_thue_morse_case_one(word, number_of_bits);
continue;
}
bool case_two = can_be_decreased(word / 2, number_of_bits - 1); // case 2: ... 2 2 1
if (case_two) {
word = undo_thue_morse_case_two(word, number_of_bits);
continue;
}
/*if (!case_one && !case_two)*/ return false;
}
return true;

// if (number_of_bits % 2 == 0) {
// // 2 2 2 2
// // 1 2 2 2 1
// } else {
// // 1 2 2 2
// // 2 2 2 1
// }
}

int count_subwords(unsigned int n) {
int count = 0;
for (int i = 0; i < 1 << n; ++i) {
if (is_thue_morse_subword(i))
++count;
}

return count;
}

#include <iostream>

int main() {
std::cout << count_subwords(14);
}
Loading

0 comments on commit 7820c1e

Please sign in to comment.