From 7820c1eb9f3ee1c113b06ad8b3f60e52ee24d8c9 Mon Sep 17 00:00:00 2001
From: witek-formanski <witekformanski@gmail.com>
Date: Mon, 20 Nov 2023 12:23:50 +0100
Subject: [PATCH] add various drafts

---
 src/cw6/zad12/fibonacci_word.cpp |  53 +++++++++
 src/cw6/zad2/lifo_stack.cpp      |  41 +++++++
 src/cw6/zad3/pond.cpp            |  18 ++-
 src/other/count_thue_morse.c     |  10 ++
 src/other/thue_morse.cpp         | 156 +++++++++++++++++++++++++
 src/other/thue_morse_char.cpp    | 188 +++++++++++++++++++++++++++++++
 6 files changed, 460 insertions(+), 6 deletions(-)
 create mode 100644 src/cw6/zad12/fibonacci_word.cpp
 create mode 100644 src/cw6/zad2/lifo_stack.cpp
 create mode 100644 src/other/count_thue_morse.c
 create mode 100644 src/other/thue_morse.cpp
 create mode 100644 src/other/thue_morse_char.cpp

diff --git a/src/cw6/zad12/fibonacci_word.cpp b/src/cw6/zad12/fibonacci_word.cpp
new file mode 100644
index 0000000..38f2e4c
--- /dev/null
+++ b/src/cw6/zad12/fibonacci_word.cpp
@@ -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";
+}
\ No newline at end of file
diff --git a/src/cw6/zad2/lifo_stack.cpp b/src/cw6/zad2/lifo_stack.cpp
new file mode 100644
index 0000000..31b9842
--- /dev/null
+++ b/src/cw6/zad2/lifo_stack.cpp
@@ -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)
+*/
\ No newline at end of file
diff --git a/src/cw6/zad3/pond.cpp b/src/cw6/zad3/pond.cpp
index e474b6e..89d6269 100644
--- a/src/cw6/zad3/pond.cpp
+++ b/src/cw6/zad3/pond.cpp
@@ -1,6 +1,6 @@
+#include <cmath>
 #include <iostream>
 #include <stack>
-#include <cmath>
 
 void freeze(std::stack<int> s, int degrees) {
     // int top = s.top();
@@ -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
+
         }
     }
 }
diff --git a/src/other/count_thue_morse.c b/src/other/count_thue_morse.c
new file mode 100644
index 0000000..c303c58
--- /dev/null
+++ b/src/other/count_thue_morse.c
@@ -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) {
+
+}
\ No newline at end of file
diff --git a/src/other/thue_morse.cpp b/src/other/thue_morse.cpp
new file mode 100644
index 0000000..1b9aba1
--- /dev/null
+++ b/src/other/thue_morse.cpp
@@ -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);
+}
\ No newline at end of file
diff --git a/src/other/thue_morse_char.cpp b/src/other/thue_morse_char.cpp
new file mode 100644
index 0000000..e4a2e7c
--- /dev/null
+++ b/src/other/thue_morse_char.cpp
@@ -0,0 +1,188 @@
+#include <algorithm>
+#include <vector>
+
+using namespace std;
+
+/*
+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(vector<int> word) {
+    int last_bit;
+    int second_last_bit;
+    while (word.size() > 1) {
+        last_bit = word.end()[-1];
+        second_last_bit = word.end()[-2];
+        if ((last_bit == 1 && second_last_bit == 1) || (last_bit == 0 && second_last_bit == 0))
+            return false;
+        word.pop_back();
+        word.pop_back();
+    }
+
+    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;
+// }
+
+vector<int> undo_thue_morse_case_one(vector<int> word) {
+    vector<int> new_word;
+    int last_bit;
+    int second_last_bit;
+    while (word.size() > 1) {
+        last_bit = word.end()[-1];
+        second_last_bit = word.end()[-2];
+        if (last_bit == 0 && second_last_bit == 1) {  // 10 -> 1
+            new_word.push_back(1);
+        } else if (last_bit == 1 && second_last_bit == 0)  // 01 -> 0
+            word.pop_back();
+        word.pop_back();
+    }
+
+    if (word.size() == 1) {
+        if (word[0] == 0) {  // 10...
+            new_word.push_back(1);
+        } else
+            new_word.push_back(0);
+    }
+
+    // reverse(new_word.begin(), new_word.end());
+
+    return new_word;
+}
+
+vector<int> undo_thue_morse_case_two(vector<int> word) {
+    vector<int> new_word;
+    int last_bit;
+    int second_last_bit;
+
+    if (word.end()[-1] == 1) {  // ...10
+        new_word.push_back(1);
+    } else
+        new_word.push_back(0);
+
+    while (word.size() > 1) {
+        last_bit = word.end()[-1];
+        second_last_bit = word.end()[-2];
+        if (last_bit == 0 && second_last_bit == 1) {  // 10 -> 1
+            new_word.push_back(1);
+        } else if (last_bit == 1 && second_last_bit == 0)  // 01 -> 0
+            word.pop_back();
+        word.pop_back();
+    }
+
+    // reverse(new_word.begin(), new_word.end());
+
+    return new_word;
+}
+
+unsigned int undo_thue_morse_case_two_old(unsigned int word, int number_of_bits) {
+    int last_two_bits;
+    unsigned int new_word;
+    int new_bits = 0;
+
+    if (number_of_bits == 1) {
+        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;
+    }
+
+    return new_word;
+}
+
+bool is_thue_morse_subword(vector<int> word) {
+    // int last_two_bits;
+    // vector<int> new_word;
+    while (word.size() > 2) {
+        bool case_one = can_be_decreased(word);  // case 1: ... 2 2 2
+        if (case_one) {
+            word = undo_thue_morse_case_one(word);
+            continue;
+        }
+        vector<int> word_pop = word;
+        word_pop.pop_back();
+        bool case_two = can_be_decreased(word_pop);  // case 2: ... 2 2 1
+        if (case_two) {
+            word = undo_thue_morse_case_two(word);
+            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) {
+        vector<int> word;
+        for (int j = 0; j < n; ++j) {
+            for (int k = 0; k <= 1; ++k) {
+                word.push_back(k);
+            }
+        }
+        if (is_thue_morse_subword(word))
+            ++count;
+    }
+
+    return count;
+}
+
+#include <iostream>
+
+int main() {
+    std::cout << count_subwords(3);
+}
\ No newline at end of file