summaryrefslogtreecommitdiff
path: root/1_anno/Programmazione_2/exercises
diff options
context:
space:
mode:
authorSanto Cariotti <dcariotti24@gmail.com>2020-10-18 18:56:43 +0200
committerSanto Cariotti <dcariotti24@gmail.com>2020-10-18 18:59:42 +0200
commit6c6328375c55683645146909b7ab760d0de0d463 (patch)
treeb18e695dfe7a10064fed111649253dc2c77208bf /1_anno/Programmazione_2/exercises
parent4e063e32250312c38d5646840719b62429362b21 (diff)
chore: name of first year folder
Diffstat (limited to '1_anno/Programmazione_2/exercises')
-rw-r--r--1_anno/Programmazione_2/exercises/carattere-maggiore.cc40
-rw-r--r--1_anno/Programmazione_2/exercises/dequeue.cc137
-rw-r--r--1_anno/Programmazione_2/exercises/doppioni.cc77
-rw-r--r--1_anno/Programmazione_2/exercises/estremi-uguali.cc32
-rw-r--r--1_anno/Programmazione_2/exercises/even-length.cc27
-rw-r--r--1_anno/Programmazione_2/exercises/exam_08_10_14.cc202
-rw-r--r--1_anno/Programmazione_2/exercises/exam_20_07_20/ex1.cpp39
-rw-r--r--1_anno/Programmazione_2/exercises/exam_20_07_20/ex2.cpp63
-rw-r--r--1_anno/Programmazione_2/exercises/exam_20_07_20/ex3.cpp284
-rw-r--r--1_anno/Programmazione_2/exercises/exam_20_07_20/ex4.cpp293
-rw-r--r--1_anno/Programmazione_2/exercises/exam_20_07_20/ex5.cpp285
-rw-r--r--1_anno/Programmazione_2/exercises/inizia-con.cc33
-rw-r--r--1_anno/Programmazione_2/exercises/inserimento-coda.cc119
-rw-r--r--1_anno/Programmazione_2/exercises/inserimento-pila.cc116
-rw-r--r--1_anno/Programmazione_2/exercises/matrice-adj.cc151
-rw-r--r--1_anno/Programmazione_2/exercises/minore.cc27
-rw-r--r--1_anno/Programmazione_2/exercises/pop-stack.cc132
-rw-r--r--1_anno/Programmazione_2/exercises/prima-maiuscola.cc30
-rw-r--r--1_anno/Programmazione_2/exercises/ripetute.cc20
-rw-r--r--1_anno/Programmazione_2/exercises/sol-ripetute.cc25
-rw-r--r--1_anno/Programmazione_2/exercises/sottosequenza.cc26
-rw-r--r--1_anno/Programmazione_2/exercises/stringa-inversa.cc26
22 files changed, 2184 insertions, 0 deletions
diff --git a/1_anno/Programmazione_2/exercises/carattere-maggiore.cc b/1_anno/Programmazione_2/exercises/carattere-maggiore.cc
new file mode 100644
index 0000000..2e89fcd
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/carattere-maggiore.cc
@@ -0,0 +1,40 @@
+#include<iostream>
+#include<map>
+#include<fstream>
+
+using namespace std;
+
+
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ string line;
+ getline(in, line);
+ map<char, int> chs;
+ for(auto const& c : line) {
+ if(c == ' ') continue;
+ if(chs.find(c) != chs.end())
+ chs[c]++;
+ else
+ chs[c] = 1;
+ }
+ int n = 0;
+ char c = 'a';
+ for(auto const& i : chs) {
+ if(i.second >= n) {
+ if(i.first >= c) {
+ n = i.second;
+ c = i.first;
+ }
+ }
+ }
+ out << c << ' ' << n << endl;
+ }
+
+ out.close();
+ in.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/dequeue.cc b/1_anno/Programmazione_2/exercises/dequeue.cc
new file mode 100644
index 0000000..4b012c4
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/dequeue.cc
@@ -0,0 +1,137 @@
+
+#include<iostream>
+#include<fstream>
+
+using namespace std;
+
+template<class T>
+class node {
+public:
+ node(T key, node<T>* next) : _key{key}, _next{next} {}
+ node(T key) : _key{key}, _next{nullptr} {}
+ const T& key() const { return _key; }
+ T& key() { return _key; }
+ const node<T>*& next() const { return _next; }
+ node<T>*& next() { return _next; }
+private:
+ T _key;
+ node<T>* _next;
+};
+
+template<class T>
+class Coda {
+public:
+ Coda() : _head{nullptr}, _tail{nullptr} {}
+ ~Coda() {
+
+ }
+ Coda<T>* enqueue(T val) {
+ if(!_head) {
+ _head = new node<T>{val, nullptr};
+ _tail = _head;
+ } else {
+ _tail->next() = new node<T>{val, nullptr};
+ _tail = _tail->next();
+ }
+
+ return this;
+ }
+ node<T>* dequeue() {
+ if(!_head) return nullptr;
+ node<T>* elem = _head;
+ delete _head;
+ _head = elem->next();
+
+ return elem;
+ }
+
+ bool is_empty() { return _head == nullptr; }
+private:
+ node<T>* _head;
+ node<T>* _tail;
+};
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ string t;
+ int n;
+ in >> t;
+ switch(t.at(0)) {
+ case 'b': {
+ Coda<bool>* queue = new Coda<bool>{};
+ in >> n;
+ string s;
+ for(int i = 0; i < n; ++i) {
+ in >> s;
+ if(s.at(0) == 'i') {
+ queue->enqueue(s.at(1) - '0');
+ } else {
+ queue->dequeue();
+ }
+ }
+ while(!queue->is_empty())
+ out << queue->dequeue()->key() << ' ';
+ out << endl;
+ break;
+ }
+ case 'd': {
+ Coda<double>* queue = new Coda<double>{};
+ in >> n;
+ string s;
+ for(int i = 0; i < n; ++i) {
+ in >> s;
+ if(s.at(0) == 'i') {
+ queue->enqueue(stod(s.substr(1, s.length()-1)));
+ } else {
+ queue->dequeue();
+ }
+ }
+ while(!queue->is_empty())
+ out << queue->dequeue()->key() << ' ';
+ out << endl;
+ break;
+ }
+ case 'c': {
+ Coda<char>* queue = new Coda<char>{};
+ in >> n;
+ string s;
+ for(int i = 0; i < n; ++i) {
+ in >> s;
+ if(s.at(0) == 'i') {
+ queue->enqueue(s.at(1));
+ } else {
+ queue->dequeue();
+ }
+ }
+ while(!queue->is_empty())
+ out << queue->dequeue()->key() << ' ';
+ out << endl;
+ break;
+ }
+ case 'i': {
+ Coda<int>* queue = new Coda<int>{};
+ in >> n;
+ string s;
+ for(int i = 0; i < n; ++i) {
+ in >> s;
+ if(s.at(0) == 'i') {
+ queue->enqueue(stoi(s.substr(1, s.length()-1)));
+ } else {
+ queue->dequeue();
+ }
+ }
+ while(!queue->is_empty())
+ out << queue->dequeue()->key() << ' ';
+ out << endl;
+ break;
+ }
+ }
+ }
+ in.close();
+ out.close();
+ return 0;
+}
+
diff --git a/1_anno/Programmazione_2/exercises/doppioni.cc b/1_anno/Programmazione_2/exercises/doppioni.cc
new file mode 100644
index 0000000..fdd3e88
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/doppioni.cc
@@ -0,0 +1,77 @@
+#include<iostream>
+#include<fstream>
+
+using namespace std;
+
+template<class T>
+class node {
+public:
+ node(T key, node* parent, node* right, node* left) : _key{key}, _parent{parent}, _right{right}, _left{left} {}
+ T& key() { return _key; }
+ const T& key() const { return _key; }
+ node<T>*& parent() { return _parent; }
+ const node<T>*& parent() const { return _parent; }
+ node<T>*& right() { return _right; }
+ const node<T>*& right() const { return _right; }
+ node<T>*& left() { return _left; }
+ const node<T>*& left() const { return _left; }
+private:
+ T _key;
+ node<T>* _parent;
+ node<T>* _right;
+ node<T>* _left;
+};
+
+class bst {
+public:
+ bst() : _duplicates{0}, _root{nullptr} {}
+ bst* add(int val) {
+ node<int>* iter = _root;
+ node<int>* prev = nullptr;
+ while(iter) {
+ prev = iter;
+ if(iter->key() == val) {
+ _duplicates++;
+ return this;
+ }
+
+ if(iter->key() > val)
+ iter = iter->right();
+ else
+ iter = iter->left();
+ }
+
+ node<int>* nodus = new node<int>{val, prev, nullptr, nullptr};
+ if(!prev)
+ _root = nodus;
+ else if(prev->key() > val)
+ prev->right() = nodus;
+ else
+ prev->left() = nodus;
+
+ return this;
+ }
+ const int& duplicates() const { return _duplicates; }
+private:
+ int _duplicates;
+ node<int>* _root;
+};
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+ for(int ts = 0; ts < 100; ++ts) {
+ bst* b = new bst{};
+ int n;
+ in >> n;
+ int e;
+ for(int i = 0; i < n; ++i) {
+ in >> e;
+ b->add(e);
+ }
+ out << b->duplicates() << endl;
+ }
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/estremi-uguali.cc b/1_anno/Programmazione_2/exercises/estremi-uguali.cc
new file mode 100644
index 0000000..63ac019
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/estremi-uguali.cc
@@ -0,0 +1,32 @@
+#include<iostream>
+#include<string>
+#include<fstream>
+
+using namespace std;
+
+int result(string s) {
+ if(s.at(0) == s.at(s.length()-1))
+ return 1;
+
+ return 0;
+}
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ int _;
+ string s;
+ int size{};
+ for(int i = 0; i < 3; ++i) {
+ in >> _ >> s;
+ size += result(s);
+ }
+ out << size << '\n';
+ }
+
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/even-length.cc b/1_anno/Programmazione_2/exercises/even-length.cc
new file mode 100644
index 0000000..2d3d6a4
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/even-length.cc
@@ -0,0 +1,27 @@
+#include<iostream>
+#include<string>
+#include<fstream>
+
+using namespace std;
+
+string result(string s) {
+ if(s.length() % 2 == 0)
+ return s;
+
+ return s.substr(0, s.length()-1);
+}
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ string s;
+ in >> s;
+ out << result(s) << '\n';
+ }
+
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/exam_08_10_14.cc b/1_anno/Programmazione_2/exercises/exam_08_10_14.cc
new file mode 100644
index 0000000..c9c33be
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/exam_08_10_14.cc
@@ -0,0 +1,202 @@
+#include<iostream>
+
+using namespace std;
+
+template<class H> class MultiBST {
+ virtual MultiBST<H>* ins(H x) = 0;
+ virtual int multiplicity(H x) = 0;
+ virtual void inorder() = 0;
+};
+
+template<class H>
+class node {
+public:
+ node(H val) : _key{val}, _parent{nullptr}, _left{nullptr}, _right{nullptr}, _rk{1} {}
+ H& key() { return _key; }
+ const H& key() const { return _key; }
+ node<H>*& parent() { return _parent; }
+ const node<H>*& parent() const { return _parent; }
+ node<H>*& left() { return _left; }
+ const node<H>*& left() const { return _left; }
+ node<H>*& right() { return _right; }
+ const node<H>*& right() const { return _right; }
+ int& rk() { return _rk; }
+ const int& rk() const { return _rk; }
+private:
+ H _key;
+ node<H>* _parent;
+ node<H>* _left;
+ node<H>* _right;
+ int _rk;
+};
+
+template<class H>
+class MyMultiBST : public MultiBST<H> {
+public:
+ MyMultiBST() : _root{nullptr} {}
+ node<H>*& root() { return _root; }
+ const node<H>*& root() const { return _root; }
+ int multiplicity(H x) {
+ auto elem = _search(x);
+ if(elem)
+ return elem->rk();
+ return 0;
+ }
+ MyMultiBST<H>* ins(H x) {
+ auto iter = _search(x);
+ if(iter) {
+ iter->rk() = iter->rk()+1;
+ } else {
+ iter = _root;
+ node<H>* y{nullptr};
+
+ while(iter) {
+ y = iter;
+ if(iter->key() > x)
+ iter = iter->left();
+ else
+ iter = iter->right();
+ }
+
+ node<H>* nodus = new node<H>{x};
+ nodus->parent() = y;
+ if(!y) {
+ _root = nodus;
+ } else if(y->key() > x) {
+ y->left() = nodus;
+ } else {
+ y->right() = nodus;
+ }
+ }
+
+ return this;
+ }
+ void inorder() {
+ _inorder(_root);
+ }
+ MyMultiBST<H>* del(H x) {
+ node<H>* y;
+ node<H>* z = _search(x);
+
+ if(!z) return this;
+
+ if(z->rk() > 1) {
+ z->rk() = z->rk()-1;
+ } else {
+ if(!z->left()) {
+ _transplant(z, z->right());
+ } else if(!z->right()) {
+ _transplant(z, z->left());
+ } else {
+ y = _min(z->right());
+ if(y->parent() != z) {
+ _transplant(y, y->right());
+ y->right() = z->right();
+ y->right()->parent() = y;
+ }
+ _transplant(z, y);
+ y->left() = z->left();
+ y->left()->parent() = y;
+ delete z;
+ }
+ }
+
+ return this;
+ }
+ node<H>* predecessor(H x) {
+ auto iter = _search(x);
+ if(!iter) return nullptr;
+ if(iter->left())
+ return _max(iter->left());
+
+ auto y = iter->parent();
+ while(y && iter == y->left()) {
+ iter = y;
+ y = y->parent();
+ }
+
+ return y;
+ }
+ int rank(H x) {
+ auto iter = predecessor(x);
+ int sum{1};
+ while(iter) {
+ sum+=iter->rk();
+ iter = predecessor(iter->key());
+ }
+ return sum;
+ }
+private:
+ void _transplant(node<H>* u, node<H>* v) {
+ if(!u->parent()) _root = v;
+ else if(u == u->parent()->left())
+ u->parent()->left() = v;
+ else
+ u->parent()->right() = v;
+
+ if(v)
+ v->parent() = u->parent();
+ }
+ node<H>* _max(node<H>* x) {
+ if(!x) return nullptr;
+
+ auto iter = x;
+ while(iter->right()) {
+ iter = iter->right();
+ }
+
+ return iter;
+ }
+ node<H>* _min(node<H>* x) {
+ if(!x) return nullptr;
+
+ auto iter = x;
+ while(iter->left()) {
+ iter = iter->left();
+ }
+
+ return iter;
+ }
+ void _inorder(node<H>* p) {
+ if(p) {
+ _inorder(p->left());
+ for(int i = 0; i < p->rk(); ++i) {
+ cout << p->key() << ' ';
+ }
+ _inorder(p->right());
+ }
+ }
+ node<H>* _search(H val) {
+ auto iter = _root;
+ while(iter && iter->key() != val) {
+ if(iter->key() > val)
+ iter = iter->left();
+ else
+ iter = iter->right();
+ }
+
+ return iter;
+ }
+ node<H>* _root;
+};
+
+int main() {
+ MyMultiBST<int>* t = new MyMultiBST<int>{};
+
+ for(auto const& i : {10, 7, 7, 23, 30, 4, 1, 5, 9, 5, 1, 7, 1, 9})
+ t->ins(i);
+
+ t->inorder();
+ cout << '\n';
+ for(auto const& i : {5, 7, 17})
+ cout << i << ": " << t->multiplicity(i) << endl;
+
+ for(auto const& i : {7, 4, 23, 7, 7})
+ t->del(i);
+ t->inorder();
+ cout << '\n';
+ for(auto const& i: {5, 9, 30})
+ cout << t->rank(i) << ' ';
+ delete t;
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/exam_20_07_20/ex1.cpp b/1_anno/Programmazione_2/exercises/exam_20_07_20/ex1.cpp
new file mode 100644
index 0000000..4f08a74
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/exam_20_07_20/ex1.cpp
@@ -0,0 +1,39 @@
+#include<iostream>
+#include<sstream>
+#include<fstream>
+#include<vector>
+
+using namespace std;
+int insertionsort(vector<int>& a, int n) {
+ int c = 0;
+ for(int i = 1; i < n; ++i) {
+ int j = i-1;
+ int key = a[i];
+ while(j > -1 && a[j] > key) {
+ swap(a[j+1], a[j]);
+ --j;
+ c++;
+ }
+ a[j+1] = key;
+ }
+ return c;
+}
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ int N;
+ in >> N;
+ int a, b;
+ vector<int> v;
+ for(int i = 0; i < N; ++i) {
+ in >> a >> b;
+ v.push_back(a+b);
+ }
+ out << insertionsort(v, v.size()) << endl;
+ }
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/exam_20_07_20/ex2.cpp b/1_anno/Programmazione_2/exercises/exam_20_07_20/ex2.cpp
new file mode 100644
index 0000000..aed25e4
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/exam_20_07_20/ex2.cpp
@@ -0,0 +1,63 @@
+#include<iostream>
+#include<sstream>
+#include<fstream>
+#include<queue>
+
+using namespace std;
+using pi = tuple<int, int, vector<int>>;
+
+class comp {
+public:
+ bool operator()(const pi& lhs, const pi& rhs) const {
+ auto xl = get<0>(lhs);
+ auto yl = get<0>(rhs);
+
+ auto il = get<1>(lhs);
+ auto jl = get<1>(rhs);
+ if(xl == yl)
+ return il > jl;
+ return xl > yl;
+ }
+};
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ int R, C;
+ in >> R >> C;
+ vector<vector<int>> v;
+ priority_queue<pi, vector<pi>, comp> pq;
+ int k;
+ for(int i = 0; i < R; ++i) {
+ v.push_back(vector<int>{});
+ for(int j = 0; j < C; ++j) {
+ in >> k;
+ v[i].push_back(k);
+ }
+ }
+
+ int index = 0;
+ for(auto const& i : v) {
+ int s = 0;
+ pi qq;
+ for(auto const& j : i)
+ s += j;
+ get<0>(qq) = s;
+ get<1>(qq) = index++;
+ get<2>(qq) = i;
+ pq.push(qq);
+ }
+ while(!pq.empty()) {
+ auto q = pq.top();
+ pq.pop();
+ for(auto const& i : get<2>(q))
+ out << i << ' ';
+ }
+ out << endl;
+ }
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/exam_20_07_20/ex3.cpp b/1_anno/Programmazione_2/exercises/exam_20_07_20/ex3.cpp
new file mode 100644
index 0000000..71f9c65
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/exam_20_07_20/ex3.cpp
@@ -0,0 +1,284 @@
+#include<iostream>
+#include<sstream>
+#include<fstream>
+
+using namespace std;
+
+template<class T>
+struct node {
+ T key;
+ node<T>* prev;
+ node<T>* right;
+ node<T>* left;
+};
+
+template<class T>
+class bst {
+public:
+ bst() : root{nullptr} , _val{0}{}
+
+ ~bst() {
+ // TODO
+ }
+
+ bst<T>* insert(initializer_list<T>&& list) {
+ for(auto const& i : list)
+ insert(i);
+
+ return this;
+ }
+
+ bst<T>* insert(T k) {
+ node<T>* nodus = new node<T>{k, nullptr, nullptr, nullptr};
+ node<T>* iter = root;
+ node<T>* prev = nullptr;
+
+ while(iter) {
+ prev = iter;
+ iter = (k < iter->key) ? iter->left : iter->right;
+ }
+
+ nodus->prev = prev;
+ if(!prev)
+ root = nodus;
+ else if(k < prev->key)
+ prev->left = nodus;
+ else
+ prev->right = nodus;
+
+ return this;
+ }
+
+ bst<T>* remove(initializer_list<T>&& list) {
+ for(auto const& i : list)
+ remove(i);
+
+ return this;
+ }
+
+ bst<T>* remove(T k) {
+ node<T>* nodus = search(k);
+ if(!nodus) return this;
+
+ if(!nodus->left) {
+ _transplant(nodus, nodus->right);
+ } else if(!nodus->right) {
+ _transplant(nodus, nodus->left);
+ } else {
+ node<T>* iter = _min(nodus->right);
+ if(iter->prev != nodus) {
+ _transplant(iter, iter->right);
+ iter->right = nodus->right;
+ iter->right->prev = iter;
+ }
+ _transplant(nodus, iter);
+ iter->left = nodus->left;
+ iter->left->prev = iter;
+ }
+
+ delete nodus;
+ return this;
+ }
+
+ node<T>* min() {
+ return _min(root);
+ }
+
+ node<T>* min(node<T>* nodus) {
+ return _min(nodus);
+ }
+
+ node<T>* max() {
+ return _max(root);
+ }
+
+ node<T>* max(node<T>* nodus) {
+ return _max(nodus);
+ }
+
+ node<T>* search(T k) {
+ node<T>* iter = root;
+ while(iter && iter->key != k)
+ iter = (iter->key > k) ? iter->left : iter->right;
+
+ return iter;
+ }
+
+ node<T>* successor(T k) {
+ node<T>* nodus = search(k);
+ if(!nodus) return nullptr;
+
+ if(nodus->right)
+ return min(nodus->right);
+
+ node<T>* prev = nodus->prev;
+ while(prev && nodus == prev->right) {
+ nodus = prev;
+ prev = prev->prev;
+ }
+
+ return prev;
+
+ }
+ node<T>* predecessor(T k) {
+ node<T>* nodus = search(k);
+ if(!nodus) return nullptr;
+
+ if(nodus->left)
+ return max(nodus->left);
+
+ node<T>* prev = nodus->prev;
+ while(prev && nodus == prev->left) {
+ nodus = prev;
+ prev = prev->prev;
+ }
+
+ return prev;
+ }
+
+ friend ostream& operator<<(ostream& os, bst<T>* tree) {
+ tree->_inorder(os, tree->root);
+ return os;
+ }
+ T sol(T k) {
+ return _max(search(k))->key;
+ }
+private:
+ int _val;
+ void _transplant(node<T>* u, node<T>* v) {
+ if(!u->prev) {
+ root = v;
+ } else if(u == u->prev->left) {
+ u->prev->left = v;
+ } else {
+ u->prev->right = v;
+ }
+
+ if(v)
+ v->prev = u->prev;
+ }
+
+ node<T>* _min(node<T>* root) {
+ node<T>* iter = root;
+ while(iter && iter->left)
+ iter = iter->left;
+
+ return iter;
+ }
+
+ node<T>* _max(node<T>* root) {
+ node<T>* iter = root;
+ while(iter && iter->right)
+ iter = iter->right;
+
+ return iter;
+ }
+
+ void _inorder(ostream& os, node<T>* root) {
+ if(root) {
+ if(root->right && root->left) {
+ _val++;
+ }
+ _inorder(os, root->left);
+ os << root->key << ' ';
+ _inorder(os, root->right);
+ }
+ }
+
+ void _preorder(ostream& os, node<T>* root) {
+ if(root) {
+ os << root->key << ' ';
+ _inorder(os, root->left);
+ _inorder(os, root->right);
+ }
+ }
+
+ void _postorder(ostream& os, node<T>* root) {
+ if(root) {
+ _inorder(os, root->left);
+ _inorder(os, root->right);
+ os << root->key << ' ';
+ }
+ }
+ node<T>* root;
+};
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ string t, comm;
+ in >> t;
+ int n;
+ switch(t.at(0)) {
+ case 'd':
+ {
+ bst<double>* b = new bst<double>{};
+ in >> n;
+ double k;
+ for(int i = 0; i < n; ++i) {
+ in >> comm;
+ stringstream tcomm(comm);
+ int ex= 0;
+ while(getline(tcomm, comm, ':')) {
+ if(ex == 0) {
+ if(comm == "ins")
+ ex = 1;
+ else
+ ex = 2;
+ } else {
+ if(ex == 1)
+ b->insert(stod(comm));
+ else
+ b->remove(stod(comm));
+ ex = 0;
+ }
+ }
+ }
+ in >> k;
+ cout << b << endl;
+ out << b->sol(k) << endl;
+
+ delete b;
+ break;
+ }
+ case 'i':
+ {
+ bst<int>* b = new bst<int>{};
+ in >> n;
+ int k;
+ for(int i = 0; i < n; ++i) {
+ in >> comm;
+ stringstream tcomm(comm);
+ int ex= 0;
+ while(getline(tcomm, comm, ':')) {
+ if(ex == 0) {
+ if(comm == "ins")
+ ex = 1;
+ else
+ ex = 2;
+ } else {
+ if(ex == 1)
+ b->insert(stoi(comm));
+ else
+ b->remove(stoi(comm));
+ ex = 0;
+ }
+ }
+ }
+ in >> k;
+ cout << b << endl;
+ out << b->sol(k) << endl;
+
+ delete b;
+ break;
+ }
+ }
+ }
+
+ in.close();
+ out.close();
+ return 0;
+}
+
diff --git a/1_anno/Programmazione_2/exercises/exam_20_07_20/ex4.cpp b/1_anno/Programmazione_2/exercises/exam_20_07_20/ex4.cpp
new file mode 100644
index 0000000..e234363
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/exam_20_07_20/ex4.cpp
@@ -0,0 +1,293 @@
+#include<iostream>
+#include<sstream>
+#include<fstream>
+
+using namespace std;
+
+template<class T>
+struct node {
+ T key;
+ node<T>* prev;
+ node<T>* right;
+ node<T>* left;
+};
+
+template<class T>
+class bst {
+public:
+ bst() : root{nullptr} , _val{0}{}
+
+ ~bst() {
+ // TODO
+ }
+
+ bst<T>* insert(initializer_list<T>&& list) {
+ for(auto const& i : list)
+ insert(i);
+
+ return this;
+ }
+
+ bst<T>* insert(T k) {
+ node<T>* nodus = new node<T>{k, nullptr, nullptr, nullptr};
+ node<T>* iter = root;
+ node<T>* prev = nullptr;
+
+ while(iter) {
+ prev = iter;
+ iter = (k < iter->key) ? iter->left : iter->right;
+ }
+
+ nodus->prev = prev;
+ if(!prev)
+ root = nodus;
+ else if(k < prev->key)
+ prev->left = nodus;
+ else
+ prev->right = nodus;
+
+ return this;
+ }
+
+ bst<T>* remove(initializer_list<T>&& list) {
+ for(auto const& i : list)
+ remove(i);
+
+ return this;
+ }
+
+ bst<T>* remove(T k) {
+ node<T>* nodus = search(k);
+ if(!nodus) return this;
+
+ if(!nodus->left) {
+ _transplant(nodus, nodus->right);
+ } else if(!nodus->right) {
+ _transplant(nodus, nodus->left);
+ } else {
+ node<T>* iter = _min(nodus->right);
+ if(iter->prev != nodus) {
+ _transplant(iter, iter->right);
+ iter->right = nodus->right;
+ iter->right->prev = iter;
+ }
+ _transplant(nodus, iter);
+ iter->left = nodus->left;
+ iter->left->prev = iter;
+ }
+
+ delete nodus;
+ return this;
+ }
+
+ node<T>* min() {
+ return _min(root);
+ }
+
+ node<T>* min(node<T>* nodus) {
+ return _min(nodus);
+ }
+
+ node<T>* max() {
+ return _max(root);
+ }
+
+ node<T>* max(node<T>* nodus) {
+ return _max(nodus);
+ }
+
+ node<T>* search(T k) {
+ node<T>* iter = root;
+ while(iter && iter->key != k)
+ iter = (iter->key > k) ? iter->left : iter->right;
+
+ return iter;
+ }
+
+ node<T>* successor(T k) {
+ node<T>* nodus = search(k);
+ if(!nodus) return nullptr;
+
+ if(nodus->right)
+ return min(nodus->right);
+
+ node<T>* prev = nodus->prev;
+ while(prev && nodus == prev->right) {
+ nodus = prev;
+ prev = prev->prev;
+ }
+
+ return prev;
+
+ }
+ node<T>* predecessor(T k) {
+ node<T>* nodus = search(k);
+ if(!nodus) return nullptr;
+
+ if(nodus->left)
+ return max(nodus->left);
+
+ node<T>* prev = nodus->prev;
+ while(prev && nodus == prev->left) {
+ nodus = prev;
+ prev = prev->prev;
+ }
+
+ return prev;
+ }
+
+ friend ostream& operator<<(ostream& os, bst<T>* tree) {
+ tree->_inorder(os, tree->root);
+ return os;
+ }
+ T sol(T k) {
+ auto x = search(k);
+ auto m = _max(x)->key;
+ auto mnn = _min(x);
+ T mn;
+ if(mnn->right)
+ mn = _min(mnn->right)->key;
+ else
+ mn = mnn->key;
+ cout << m << ' ' << mn << endl;
+ return m - mn;
+ }
+private:
+ int _val;
+ void _transplant(node<T>* u, node<T>* v) {
+ if(!u->prev) {
+ root = v;
+ } else if(u == u->prev->left) {
+ u->prev->left = v;
+ } else {
+ u->prev->right = v;
+ }
+
+ if(v)
+ v->prev = u->prev;
+ }
+
+ node<T>* _min(node<T>* root) {
+ node<T>* iter = root;
+ while(iter && iter->left)
+ iter = iter->left;
+
+ return iter;
+ }
+
+ node<T>* _max(node<T>* root) {
+ node<T>* iter = root;
+ while(iter && iter->right)
+ iter = iter->right;
+
+ return iter;
+ }
+
+ void _inorder(ostream& os, node<T>* root) {
+ if(root) {
+ if(root->right && root->left) {
+ _val++;
+ }
+ _inorder(os, root->left);
+ os << root->key << ' ';
+ _inorder(os, root->right);
+ }
+ }
+
+ void _preorder(ostream& os, node<T>* root) {
+ if(root) {
+ os << root->key << ' ';
+ _inorder(os, root->left);
+ _inorder(os, root->right);
+ }
+ }
+
+ void _postorder(ostream& os, node<T>* root) {
+ if(root) {
+ _inorder(os, root->left);
+ _inorder(os, root->right);
+ os << root->key << ' ';
+ }
+ }
+ node<T>* root;
+};
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ string t, comm;
+ in >> t;
+ int n;
+ switch(t.at(0)) {
+ case 'd':
+ {
+ bst<double>* b = new bst<double>{};
+ in >> n;
+ double k;
+ for(int i = 0; i < n; ++i) {
+ in >> comm;
+ stringstream tcomm(comm);
+ int ex= 0;
+ while(getline(tcomm, comm, ':')) {
+ if(ex == 0) {
+ if(comm == "ins")
+ ex = 1;
+ else
+ ex = 2;
+ } else {
+ if(ex == 1)
+ b->insert(stod(comm));
+ else
+ b->remove(stod(comm));
+ ex = 0;
+ }
+ }
+ }
+ in >> k;
+ cout << b << endl;
+ out << b->sol(k) << endl;
+
+ delete b;
+ break;
+ }
+ case 'i':
+ {
+ bst<int>* b = new bst<int>{};
+ in >> n;
+ int k;
+ for(int i = 0; i < n; ++i) {
+ in >> comm;
+ stringstream tcomm(comm);
+ int ex= 0;
+ while(getline(tcomm, comm, ':')) {
+ if(ex == 0) {
+ if(comm == "ins")
+ ex = 1;
+ else
+ ex = 2;
+ } else {
+ if(ex == 1)
+ b->insert(stoi(comm));
+ else
+ b->remove(stoi(comm));
+ ex = 0;
+ }
+ }
+ }
+ in >> k;
+ cout << b << endl;
+ out << b->sol(k) << endl;
+
+ delete b;
+ break;
+ }
+ }
+ }
+
+ in.close();
+ out.close();
+ return 0;
+}
+
diff --git a/1_anno/Programmazione_2/exercises/exam_20_07_20/ex5.cpp b/1_anno/Programmazione_2/exercises/exam_20_07_20/ex5.cpp
new file mode 100644
index 0000000..dd8fc98
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/exam_20_07_20/ex5.cpp
@@ -0,0 +1,285 @@
+#include<iostream>
+#include<vector>
+#include<algorithm>
+#include<queue>
+#include<stack>
+#include<fstream>
+#define INF 99999999
+#define W -1
+#define G 0
+#define B 1
+
+using namespace std;
+
+template<class H>
+class graph {
+public:
+ graph(int len) : _len{len}, _nodes{0}, _edges{0} {
+ _k = new H*[len];
+ _adj = new vector<int>[len];
+ _colors = new int[len];
+ _d = new int[len];
+ _f = new int[len];
+ _p = new int[len];
+
+ for(int i = 0; i < len; ++i) {
+ _k[i] = nullptr;
+ }
+ }
+ ~graph() {
+ delete[] _k;
+ delete[] _adj;
+ }
+
+ graph<H>* add_node(H x) {
+ if(_index(x) > -1) return this;
+ if(_nodes == _len) return this;
+ _k[_nodes++] = new H(x);
+
+ return this;
+ }
+ graph<H>* add_edge(H u, H v) {
+ int i = _index(u);
+ int j = _index(v);
+ if(i < 0 || j < 0) return this;
+
+ if(find(_adj[i].begin(), _adj[i].end(), j) == _adj[i].end()) {
+ _adj[i].push_back(j);
+ }
+ return this;
+ }
+ void print() {
+ for(int i = 0; i < _nodes; ++i) {
+ cout << "(" << *_k[i] << ", " << i << "): ";
+ for(auto const& j : _adj[i]) {
+ cout << "(" << *_k[j] << ", " << j << ") ";
+ }
+ cout << endl;
+ }
+ }
+ void dfsvisit(int u, bool visited[]) {
+ visited[u] = true;
+ cout << "(" << *_k[u] << ", " << u << ") ";
+ for(auto const& i : _adj[u]) {
+ if(!visited[i])
+ dfsvisit(i, visited);
+ }
+ }
+ int dfsvisit(int u) {
+ int cycle = 0;
+ _colors[u] = G;
+ _d[u] = _time++;
+
+ for(auto const& j : _adj[u]) {
+ if(_colors[j] == W)
+ cycle |= dfsvisit(j);
+ }
+
+ _colors[u] = B;
+ _stack.push(u);
+ _f[u] = _time++;
+ return cycle;
+ }
+ int dfs() {
+ int cycle = 0;
+ for(int i = 0; i < _nodes; ++i) {
+ _colors[i] = W;
+ }
+ _time = 0;
+ for(int i = 0; i < _nodes; ++i) {
+ if(_colors[i] == W)
+ cycle |= dfsvisit(i);
+ else if(_colors[i] == G)
+ cycle = 1;
+ }
+ for(int i = 0; i < _nodes; ++i) {
+ cout << "(" << *_k[i] << ", " << i << "): [" << _d[i] << "," << _f[i] << "]" << endl;
+ }
+ return cycle;
+ }
+
+ void top_sort() {
+ int cycle = dfs();
+ if(cycle) {
+ cout << "cyclic graph!" << endl;
+ return;
+ }
+ int* s = new int[_nodes];
+ for(int i = 0; i < _nodes; ++i) s[i] = i;
+ _sort(s, _nodes, _f);
+ for(int i = 0; i < _nodes; ++i) {
+ cout << "(" << s[i] << ", " << _f[s[i]] << ") ";
+ }
+
+ }
+
+ void bfs(H v) {
+ int s = _index(v);
+ if(s < 0) return;
+
+ for(int i = 0; i < _nodes; ++i) {
+ _colors[i] = W;
+ _d[i] = INF;
+ _p[i] = -1;
+ }
+ _colors[s] = G;
+ _d[s] = 0;
+ queue<int> q;
+ q.push(s);
+ while(!q.empty()) {
+ int u = q.front();
+ q.pop();
+ for(auto const& j : _adj[u]) {
+ if(_colors[j] == W) {
+ _colors[j] = G;
+ _d[j] = _d[u]+1;
+ _p[j] = u;
+ q.push(j);
+ }
+ }
+ _colors[u] = B;
+ }
+ for(int i = 0; i < _nodes; ++i) {
+ cout << "(" << *_k[i] << ", " << i << "): [" << _d[i] << "]" << endl;
+ }
+ }
+ int ssc() {
+ dfs();
+ cout << endl << endl;
+ bool* visited = new bool[_nodes];
+ for(int i = 0; i < _nodes; ++i)
+ visited[i] = false;
+ int count = 0;
+ while(!_stack.empty()) {
+ int v = _stack.top();
+ _stack.pop();
+ if(!visited[v]) {
+ dfsvisit(v, visited);
+ count++;
+ cout << endl;
+ }
+ }
+ delete[] visited;
+ return count;
+ }
+private:
+ graph<H>* _transpose() {
+ graph<H>* g = new graph<H>{_len};
+ for(int i = 0; i < _nodes; ++i) {
+ g->add_node(*_k[i]);
+ }
+
+ for(int i = 0; i < _nodes; ++i) {
+ for(auto const& j : _adj[i]) {
+ g->add_edge(j, i);
+ }
+ }
+
+ return g;
+ }
+ void _sort(int* d, int n, int* s) {
+ for(int i = -1; i < n; ++i) {
+ int j = i-1;
+ while(j > -1 && s[d[j+1]]>s[d[j]]) {
+ swap(d[s[j+1]], d[s[j]]);
+ --j;
+ }
+ }
+ }
+ int _index(H x) {
+ for(int i = 0; i < _nodes; ++i)
+ if(*_k[i] == x) return i;
+
+ return -1;
+ }
+ stack<int> _stack;
+ H** _k;
+ vector<int>* _adj;
+ int _len, _nodes, _edges;
+ int* _colors;
+ int _time;
+ int* _d;
+ int* _f;
+ int* _p;
+};
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ int n, e;
+ in >> n>>e;
+ string t;
+ in >> t;
+ char _t;
+ switch(t.at(0)){
+ case 'd':{
+ graph<double>* g = new graph<double>{n};
+ double x, y;
+ for(int i = 0; i < n; ++i) {
+ in >> x;
+ g->add_node(x);
+ }
+ for(int i = 0; i < e; ++i) {
+ in >>_t;
+ in >> x >> y;
+ in >> _t;
+ g->add_edge(x, y);
+ g->add_edge(y, x);
+ }
+ g->print();
+ cout << endl << endl;
+ out << g->ssc()<<endl;
+ delete g;
+ break;
+ }
+ case 'i':{
+
+ graph<int>* g = new graph<int>{n};
+ int x, y;
+ for(int i = 0; i < n; ++i) {
+ in >> x;
+ g->add_node(x);
+ }
+ for(int i = 0; i < e; ++i) {
+ in >>_t;
+ in >> x >> y;
+ in >> _t;
+ g->add_edge(x, y);
+ g->add_edge(y, x);
+ }
+ g->print();
+ cout << endl << endl;
+ out << g->ssc()<<endl;
+ delete g;
+ break;
+ }
+ case 'c': {
+
+ graph<char>* g = new graph<char>{n};
+ char x, y;
+ for(int i = 0; i < n; ++i) {
+ in >> x;
+ g->add_node(x);
+ }
+ for(int i = 0; i < e; ++i) {
+ in >>_t;
+ in >> x >> y;
+ in >> _t;
+ g->add_edge(x, y);
+ g->add_edge(y, x);
+ }
+ g->print();
+ cout << endl << endl;
+ out << g->ssc()<<endl;
+ delete g;
+ break;
+ }
+ }
+ }
+ in.close();
+ out.close();
+ return 0;
+}
+
diff --git a/1_anno/Programmazione_2/exercises/inizia-con.cc b/1_anno/Programmazione_2/exercises/inizia-con.cc
new file mode 100644
index 0000000..97f0c39
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/inizia-con.cc
@@ -0,0 +1,33 @@
+#include<iostream>
+#include<string>
+#include<fstream>
+
+using namespace std;
+
+string result(string s, char c) {
+ if(s.at(0) == c)
+ return s + ' ';
+
+ return "";
+}
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ int _;
+ string s;
+ char c;
+ in >> c;
+ for(int i = 0; i < 3; ++i) {
+ in >> _ >> s;
+ out << result(s, c);
+ }
+ out << '\n';
+ }
+
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/inserimento-coda.cc b/1_anno/Programmazione_2/exercises/inserimento-coda.cc
new file mode 100644
index 0000000..6775ca2
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/inserimento-coda.cc
@@ -0,0 +1,119 @@
+#include<iostream>
+#include<fstream>
+
+using namespace std;
+
+template<class T>
+class node {
+public:
+ node(T key, node<T>* next) : _key{key}, _next{next} {}
+ node(T key) : _key{key}, _next{nullptr} {}
+ const T& key() const { return _key; }
+ T& key() { return _key; }
+ const node<T>*& next() const { return _next; }
+ node<T>*& next() { return _next; }
+private:
+ T _key;
+ node<T>* _next;
+};
+
+template<class T>
+class Coda {
+public:
+ Coda() : _head{nullptr}, _tail{nullptr} {}
+ ~Coda() {
+
+ }
+ Coda<T>* enqueue(T val) {
+ if(!_head) {
+ _head = new node<T>{val, nullptr};
+ _tail = _head;
+ } else {
+ _tail->next() = new node<T>{val, nullptr};
+ _tail = _tail->next();
+ }
+
+ return this;
+ }
+ node<T>* pop() {
+ if(!_head) return nullptr;
+ node<T>* elem = _head;
+ delete _head;
+ _head = elem->next();
+
+ return elem;
+ }
+
+ bool is_empty() { return _head == nullptr; }
+private:
+ node<T>* _head;
+ node<T>* _tail;
+};
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ string t;
+ int n;
+ in >> t;
+ switch(t.at(0)) {
+ case 'b': {
+ Coda<bool>* queue = new Coda<bool>{};
+ in >> n;
+ bool e;
+ for(int i = 0; i < n; ++i) {
+ in >> e;
+ queue->enqueue(e);
+ }
+ while(!queue->is_empty())
+ out << queue->pop()->key() << ' ';
+ out << endl;
+ break;
+ }
+ case 'd': {
+ Coda<double>* queue = new Coda<double>{};
+ in >> n;
+ double e;
+ for(int i = 0; i < n; ++i) {
+ in >> e;
+ queue->enqueue(e);
+ }
+ while(!queue->is_empty())
+ out << queue->pop()->key() << ' ';
+ out << endl;
+ break;
+ }
+ case 'c': {
+ Coda<char>* queue = new Coda<char>{};
+ in >> n;
+ char e;
+ for(int i = 0; i < n; ++i) {
+ in >> e;
+ queue->enqueue(e);
+ }
+ while(!queue->is_empty())
+ out << queue->pop()->key() << ' ';
+ out << endl;
+ break;
+ }
+ case 'i': {
+ Coda<int>* queue = new Coda<int>{};
+ in >> n;
+ int e;
+ for(int i = 0; i < n; ++i) {
+ in >> e;
+ queue->enqueue(e);
+ }
+ while(!queue->is_empty())
+ out << queue->pop()->key() << ' ';
+ out << endl;
+ break;
+ }
+ }
+ }
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/inserimento-pila.cc b/1_anno/Programmazione_2/exercises/inserimento-pila.cc
new file mode 100644
index 0000000..0dbe99b
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/inserimento-pila.cc
@@ -0,0 +1,116 @@
+#include<iostream>
+#include<fstream>
+
+using namespace std;
+
+template<class T>
+class node {
+public:
+ node(T key, node<T>* next) : _key{key}, _next{next} {}
+ node(T key) : _key{key}, _next{nullptr} {}
+ const T& key() const { return _key; }
+ T& key() { return _key; }
+ const node<T>*& next() const { return _next; }
+ node<T>*& next() { return _next; }
+private:
+ T _key;
+ node<T>* _next;
+};
+
+template<class T>
+class Pila {
+public:
+ Pila() : _head{nullptr}{}
+ ~Pila() {
+
+ }
+ Pila<T>* push(T val) {
+ if(!_head) {
+ _head = new node<T>{val, nullptr};
+ } else {
+ _head = new node<T>{val, _head};
+ }
+
+ return this;
+ }
+ node<T>* pop() {
+ if(!_head) return nullptr;
+ node<T>* elem = _head;
+ delete _head;
+ _head = elem->next();
+
+ return elem;
+ }
+
+ bool is_empty() { return _head == nullptr; }
+private:
+ node<T>* _head;
+};
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ string t;
+ int n;
+ in >> t;
+ switch(t.at(0)) {
+ case 'b': {
+ Pila<bool>* stack = new Pila<bool>{};
+ in >> n;
+ bool e;
+ for(int i = 0; i < n; ++i) {
+ in >> e;
+ stack->push(e);
+ }
+ while(!stack->is_empty())
+ out << stack->pop()->key() << ' ';
+ out << endl;
+ break;
+ }
+ case 'd': {
+ Pila<double>* stack = new Pila<double>{};
+ in >> n;
+ double e;
+ for(int i = 0; i < n; ++i) {
+ in >> e;
+ stack->push(e);
+ }
+ while(!stack->is_empty())
+ out << stack->pop()->key() << ' ';
+ out << endl;
+ break;
+ }
+ case 'c': {
+ Pila<char>* stack = new Pila<char>{};
+ in >> n;
+ char e;
+ for(int i = 0; i < n; ++i) {
+ in >> e;
+ stack->push(e);
+ }
+ while(!stack->is_empty())
+ out << stack->pop()->key() << ' ';
+ out << endl;
+ break;
+ }
+ case 'i': {
+ Pila<int>* stack = new Pila<int>{};
+ in >> n;
+ int e;
+ for(int i = 0; i < n; ++i) {
+ in >> e;
+ stack->push(e);
+ }
+ while(!stack->is_empty())
+ out << stack->pop()->key() << ' ';
+ out << endl;
+ break;
+ }
+ }
+ }
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/matrice-adj.cc b/1_anno/Programmazione_2/exercises/matrice-adj.cc
new file mode 100644
index 0000000..7830f97
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/matrice-adj.cc
@@ -0,0 +1,151 @@
+#include<iostream>
+#include<vector>
+#include<fstream>
+#include<sstream>
+
+using namespace std;
+
+template<class H>
+class matrix_graph {
+public:
+ ~matrix_graph() {
+ delete[] _m;
+ delete[] _k;
+ }
+ matrix_graph(int len) : _len{len}, _nodes{0}, _edges{0} {
+ _m = new int*[len];
+ _k = new H*[len];
+ for(int i = 0; i < len; ++i) {
+ _m[i] = new int[len];
+ _k[i] = nullptr;
+ for(int j = 0; j < len; ++j)
+ _m[i][j] = 0;
+ }
+ }
+
+ matrix_graph<H>* add_node(H k) {
+ if(_len == _nodes) return this; // no more space for new nodes
+ if(_index(k) > -1) return this; // nodes already exists
+
+ _k[_nodes++] = new H(k);
+
+ return this;
+ }
+
+ matrix_graph<H>* add_edge(H x, H y) {
+ int i = _index(x);
+ int j = _index(y);
+ if(i < 0 || j < 0) return this;
+ if(!_m[i][j]) {
+ _m[i][j] = 1;
+ _edges++;
+ }
+ return this;
+ }
+ vector<vector<H>> print() {
+ vector<vector<H>> v;
+ for(int i = 0; i < _len; ++i) {
+ v.push_back(vector<H>{*_k[i]});
+ vector<H> temp;
+ for(int j = 0; j < _len; ++j) {
+ if(_m[i][j]) {
+ temp.push_back(*_k[j]);
+ }
+ }
+ sort(begin(temp), end(temp));
+ for(auto const& j : temp)
+ v[i].push_back(j);
+ }
+
+ sort(begin(v), end(v));
+ return v;
+ }
+private:
+ int _len, _nodes, _edges;
+ int** _m;
+ H** _k;
+ int _index(H x) {
+ for(int i = 0; i < _nodes; ++i)
+ if(*_k[i] == x) return i;
+ return -1;
+ }
+};
+
+template<class H>
+string result(vector<vector<H>> v) {
+ string s{""};
+ ostringstream ss;
+ for(auto const& i : v) {
+ s += "(";
+ for(auto const& j : i) {
+ ss << j;
+ s+= ss.str();
+ s+= ' ';
+ ss.str(""); ss.clear();
+ }
+ s.pop_back();
+ s += ") ";
+ }
+ return s;
+}
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+ for(int ts = 0; ts < 100; ++ts) {
+ int n, m;
+ in >> n >> m;
+ string t;
+ in >> t;
+ if(t == "int") {
+ matrix_graph<int>* g = new matrix_graph<int>(n);
+ int x,y;
+ for(int i = 0; i < n; ++i) {
+ in >> x;
+ g->add_node(x);
+ }
+ char ex;
+ for(int i = 0; i < m; ++i) {
+ in >> ex >> x >> y >> ex;
+ g->add_edge(x, y);
+ }
+ auto v = g->print();
+ out << result(v) << endl;
+ delete g;
+ } else if(t == "double") {
+ matrix_graph<double>* g = new matrix_graph<double>(n);
+ double x,y;
+ for(int i = 0; i < n; ++i) {
+ in >> x;
+ g->add_node(x);
+ }
+ char ex;
+ for(int i = 0; i < m; ++i) {
+ in >> ex >> x >> y >> ex;
+ g->add_edge(x, y);
+ }
+ auto v = g->print();
+ out << result(v) << endl;
+ delete g;
+ } else {
+ matrix_graph<char>* g = new matrix_graph<char>(n);
+ char x,y;
+ for(int i = 0; i < n; ++i) {
+ in >> x;
+ g->add_node(x);
+ }
+ char ex;
+ for(int i = 0; i < m; ++i) {
+ in >> ex >> x >> y >> ex;
+ g->add_edge(x, y);
+ }
+
+ auto v = g->print();
+ out << result(v) << endl;
+ delete g;
+ }
+ }
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/minore.cc b/1_anno/Programmazione_2/exercises/minore.cc
new file mode 100644
index 0000000..677da76
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/minore.cc
@@ -0,0 +1,27 @@
+#include<iostream>
+#include<string>
+#include<fstream>
+
+using namespace std;
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ string s;
+ in >> s;
+ int minor = stoi(s);
+ for(in >> s; s != "STOP"; in >> s) {
+ int si = stoi(s);
+ if(si < minor)
+ minor = si;
+ }
+
+ out << minor << '\n';
+ }
+
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/pop-stack.cc b/1_anno/Programmazione_2/exercises/pop-stack.cc
new file mode 100644
index 0000000..3739450
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/pop-stack.cc
@@ -0,0 +1,132 @@
+#include<iostream>
+#include<fstream>
+
+using namespace std;
+
+template<class T>
+class node {
+public:
+ node(T key, node<T>* next) : _key{key}, _next{next} {}
+ node(T key) : _key{key}, _next{nullptr} {}
+ const T& key() const { return _key; }
+ T& key() { return _key; }
+ const node<T>*& next() const { return _next; }
+ node<T>*& next() { return _next; }
+private:
+ T _key;
+ node<T>* _next;
+};
+
+template<class T>
+class Pila {
+public:
+ Pila() : _head{nullptr}{}
+ ~Pila() {
+
+ }
+ Pila<T>* push(T val) {
+ if(!_head) {
+ _head = new node<T>{val, nullptr};
+ } else {
+ _head = new node<T>{val, _head};
+ }
+
+ return this;
+ }
+ node<T>* pop() {
+ if(!_head) return nullptr;
+ node<T>* elem = _head;
+ delete _head;
+ _head = elem->next();
+
+ return elem;
+ }
+
+ bool is_empty() { return _head == nullptr; }
+private:
+ node<T>* _head;
+};
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ string t;
+ int n;
+ in >> t;
+ switch(t.at(0)) {
+ case 'b': {
+ Pila<bool>* stack = new Pila<bool>{};
+ in >> n;
+ string s;
+ for(int i = 0; i < n; ++i) {
+ in >> s;
+ if(s.at(0) == 'i') {
+ stack->push(s.at(1) - '0');
+ } else {
+ stack->pop();
+ }
+ }
+ while(!stack->is_empty())
+ out << stack->pop()->key() << ' ';
+ out << endl;
+ break;
+ }
+ case 'd': {
+ Pila<double>* stack = new Pila<double>{};
+ in >> n;
+ string s;
+ for(int i = 0; i < n; ++i) {
+ in >> s;
+ if(s.at(0) == 'i') {
+ stack->push(stod(s.substr(1, s.length()-1)));
+ } else {
+ stack->pop();
+ }
+ }
+ while(!stack->is_empty())
+ out << stack->pop()->key() << ' ';
+ out << endl;
+ break;
+ }
+ case 'c': {
+ Pila<char>* stack = new Pila<char>{};
+ in >> n;
+ string s;
+ for(int i = 0; i < n; ++i) {
+ in >> s;
+ if(s.at(0) == 'i') {
+ stack->push(s.at(1));
+ } else {
+ stack->pop();
+ }
+ }
+ while(!stack->is_empty())
+ out << stack->pop()->key() << ' ';
+ out << endl;
+ break;
+ }
+ case 'i': {
+ Pila<int>* stack = new Pila<int>{};
+ in >> n;
+ string s;
+ for(int i = 0; i < n; ++i) {
+ in >> s;
+ if(s.at(0) == 'i') {
+ stack->push(stoi(s.substr(1, s.length()-1)));
+ } else {
+ stack->pop();
+ }
+ }
+ while(!stack->is_empty())
+ out << stack->pop()->key() << ' ';
+ out << endl;
+ break;
+ }
+ }
+ }
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/prima-maiuscola.cc b/1_anno/Programmazione_2/exercises/prima-maiuscola.cc
new file mode 100644
index 0000000..6273219
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/prima-maiuscola.cc
@@ -0,0 +1,30 @@
+#include<iostream>
+#include<string>
+#include<fstream>
+
+using namespace std;
+
+string result(string s) {
+ s.at(0) = toupper(s.at(0));
+
+ return s;
+}
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ int _;
+ string s;
+ for(int i = 0; i < 3; ++i) {
+ in >> _ >> s;
+ out << result(s) << ' ';
+ }
+ out << '\n';
+ }
+
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/ripetute.cc b/1_anno/Programmazione_2/exercises/ripetute.cc
new file mode 100644
index 0000000..23d151a
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/ripetute.cc
@@ -0,0 +1,20 @@
+#include<iostream>
+#include<string>
+#include<fstream>
+
+using namespace std;
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ string s;
+ in >> s;
+ out << s+s << '\n';
+ }
+
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/sol-ripetute.cc b/1_anno/Programmazione_2/exercises/sol-ripetute.cc
new file mode 100644
index 0000000..1518d76
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/sol-ripetute.cc
@@ -0,0 +1,25 @@
+#include<iostream>
+#include<string>
+#include<fstream>
+
+using namespace std;
+
+string result(string s) {
+ int half = s.length()/2;
+ return s.substr(0, half);
+}
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ string s;
+ in >> s;
+ out << result(s) << '\n';
+ }
+
+ in.close();
+ out.close();
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/sottosequenza.cc b/1_anno/Programmazione_2/exercises/sottosequenza.cc
new file mode 100644
index 0000000..1d7c481
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/sottosequenza.cc
@@ -0,0 +1,26 @@
+#include<iostream>
+#include<fstream>
+
+using namespace std;
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ int n; string s;
+ in >> n >> s;
+ string tm;
+ for(int i = 0; i < n; ++i) {
+ in >> tm;
+ if(tm.find(s) != string::npos)
+ out << tm << ' ';
+ }
+ out << '\n';
+ }
+
+ in.close();
+ out.close();
+
+ return 0;
+}
diff --git a/1_anno/Programmazione_2/exercises/stringa-inversa.cc b/1_anno/Programmazione_2/exercises/stringa-inversa.cc
new file mode 100644
index 0000000..5c37718
--- /dev/null
+++ b/1_anno/Programmazione_2/exercises/stringa-inversa.cc
@@ -0,0 +1,26 @@
+#include<iostream>
+#include<fstream>
+
+using namespace std;
+
+int main() {
+ ifstream in("input.txt");
+ ofstream out("output.txt");
+
+ for(int ts = 0; ts < 100; ++ts) {
+ int n;
+ in >> n;
+ for(int i = 0; i < 3; ++i) {
+ string s;
+ in >> s;
+ for(int j = s.length()-1; j > -1; --j) {
+ out << s.at(j);
+ }
+ out << ' ';
+ }
+ out << endl;
+ }
+
+ out.close();
+ in.close();
+}