From d2edbc38cac8da52f58c5cd3da6c0c625fa05736 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 6 Feb 2021 19:56:36 +0100 Subject: conf: rename --- .../Programming_2/exercises/carattere-maggiore.cc | 40 +++ Year_1/Programming_2/exercises/dequeue.cc | 137 ++++++++++ Year_1/Programming_2/exercises/doppioni.cc | 77 ++++++ Year_1/Programming_2/exercises/estremi-uguali.cc | 32 +++ Year_1/Programming_2/exercises/even-length.cc | 27 ++ Year_1/Programming_2/exercises/exam_08_10_14.cc | 202 ++++++++++++++ .../Programming_2/exercises/exam_20_07_20/ex1.cpp | 39 +++ .../Programming_2/exercises/exam_20_07_20/ex2.cpp | 63 +++++ .../Programming_2/exercises/exam_20_07_20/ex3.cpp | 284 ++++++++++++++++++++ .../Programming_2/exercises/exam_20_07_20/ex4.cpp | 293 +++++++++++++++++++++ .../Programming_2/exercises/exam_20_07_20/ex5.cpp | 285 ++++++++++++++++++++ Year_1/Programming_2/exercises/inizia-con.cc | 33 +++ Year_1/Programming_2/exercises/inserimento-coda.cc | 119 +++++++++ Year_1/Programming_2/exercises/inserimento-pila.cc | 116 ++++++++ Year_1/Programming_2/exercises/matrice-adj.cc | 151 +++++++++++ Year_1/Programming_2/exercises/minore.cc | 27 ++ Year_1/Programming_2/exercises/pop-stack.cc | 132 ++++++++++ Year_1/Programming_2/exercises/prima-maiuscola.cc | 30 +++ Year_1/Programming_2/exercises/ripetute.cc | 20 ++ Year_1/Programming_2/exercises/sol-ripetute.cc | 25 ++ Year_1/Programming_2/exercises/sottosequenza.cc | 26 ++ Year_1/Programming_2/exercises/stringa-inversa.cc | 26 ++ 22 files changed, 2184 insertions(+) create mode 100644 Year_1/Programming_2/exercises/carattere-maggiore.cc create mode 100644 Year_1/Programming_2/exercises/dequeue.cc create mode 100644 Year_1/Programming_2/exercises/doppioni.cc create mode 100644 Year_1/Programming_2/exercises/estremi-uguali.cc create mode 100644 Year_1/Programming_2/exercises/even-length.cc create mode 100644 Year_1/Programming_2/exercises/exam_08_10_14.cc create mode 100644 Year_1/Programming_2/exercises/exam_20_07_20/ex1.cpp create mode 100644 Year_1/Programming_2/exercises/exam_20_07_20/ex2.cpp create mode 100644 Year_1/Programming_2/exercises/exam_20_07_20/ex3.cpp create mode 100644 Year_1/Programming_2/exercises/exam_20_07_20/ex4.cpp create mode 100644 Year_1/Programming_2/exercises/exam_20_07_20/ex5.cpp create mode 100644 Year_1/Programming_2/exercises/inizia-con.cc create mode 100644 Year_1/Programming_2/exercises/inserimento-coda.cc create mode 100644 Year_1/Programming_2/exercises/inserimento-pila.cc create mode 100644 Year_1/Programming_2/exercises/matrice-adj.cc create mode 100644 Year_1/Programming_2/exercises/minore.cc create mode 100644 Year_1/Programming_2/exercises/pop-stack.cc create mode 100644 Year_1/Programming_2/exercises/prima-maiuscola.cc create mode 100644 Year_1/Programming_2/exercises/ripetute.cc create mode 100644 Year_1/Programming_2/exercises/sol-ripetute.cc create mode 100644 Year_1/Programming_2/exercises/sottosequenza.cc create mode 100644 Year_1/Programming_2/exercises/stringa-inversa.cc (limited to 'Year_1/Programming_2/exercises') diff --git a/Year_1/Programming_2/exercises/carattere-maggiore.cc b/Year_1/Programming_2/exercises/carattere-maggiore.cc new file mode 100644 index 0000000..2e89fcd --- /dev/null +++ b/Year_1/Programming_2/exercises/carattere-maggiore.cc @@ -0,0 +1,40 @@ +#include +#include +#include + +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 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/Year_1/Programming_2/exercises/dequeue.cc b/Year_1/Programming_2/exercises/dequeue.cc new file mode 100644 index 0000000..4b012c4 --- /dev/null +++ b/Year_1/Programming_2/exercises/dequeue.cc @@ -0,0 +1,137 @@ + +#include +#include + +using namespace std; + +template +class node { +public: + node(T key, node* next) : _key{key}, _next{next} {} + node(T key) : _key{key}, _next{nullptr} {} + const T& key() const { return _key; } + T& key() { return _key; } + const node*& next() const { return _next; } + node*& next() { return _next; } +private: + T _key; + node* _next; +}; + +template +class Coda { +public: + Coda() : _head{nullptr}, _tail{nullptr} {} + ~Coda() { + + } + Coda* enqueue(T val) { + if(!_head) { + _head = new node{val, nullptr}; + _tail = _head; + } else { + _tail->next() = new node{val, nullptr}; + _tail = _tail->next(); + } + + return this; + } + node* dequeue() { + if(!_head) return nullptr; + node* elem = _head; + delete _head; + _head = elem->next(); + + return elem; + } + + bool is_empty() { return _head == nullptr; } +private: + node* _head; + node* _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* queue = new Coda{}; + 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* queue = new Coda{}; + 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* queue = new Coda{}; + 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* queue = new Coda{}; + 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/Year_1/Programming_2/exercises/doppioni.cc b/Year_1/Programming_2/exercises/doppioni.cc new file mode 100644 index 0000000..fdd3e88 --- /dev/null +++ b/Year_1/Programming_2/exercises/doppioni.cc @@ -0,0 +1,77 @@ +#include +#include + +using namespace std; + +template +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*& parent() { return _parent; } + const node*& parent() const { return _parent; } + node*& right() { return _right; } + const node*& right() const { return _right; } + node*& left() { return _left; } + const node*& left() const { return _left; } +private: + T _key; + node* _parent; + node* _right; + node* _left; +}; + +class bst { +public: + bst() : _duplicates{0}, _root{nullptr} {} + bst* add(int val) { + node* iter = _root; + node* 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* nodus = new node{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* _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/Year_1/Programming_2/exercises/estremi-uguali.cc b/Year_1/Programming_2/exercises/estremi-uguali.cc new file mode 100644 index 0000000..63ac019 --- /dev/null +++ b/Year_1/Programming_2/exercises/estremi-uguali.cc @@ -0,0 +1,32 @@ +#include +#include +#include + +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/Year_1/Programming_2/exercises/even-length.cc b/Year_1/Programming_2/exercises/even-length.cc new file mode 100644 index 0000000..2d3d6a4 --- /dev/null +++ b/Year_1/Programming_2/exercises/even-length.cc @@ -0,0 +1,27 @@ +#include +#include +#include + +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/Year_1/Programming_2/exercises/exam_08_10_14.cc b/Year_1/Programming_2/exercises/exam_08_10_14.cc new file mode 100644 index 0000000..c9c33be --- /dev/null +++ b/Year_1/Programming_2/exercises/exam_08_10_14.cc @@ -0,0 +1,202 @@ +#include + +using namespace std; + +template class MultiBST { + virtual MultiBST* ins(H x) = 0; + virtual int multiplicity(H x) = 0; + virtual void inorder() = 0; +}; + +template +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*& parent() { return _parent; } + const node*& parent() const { return _parent; } + node*& left() { return _left; } + const node*& left() const { return _left; } + node*& right() { return _right; } + const node*& right() const { return _right; } + int& rk() { return _rk; } + const int& rk() const { return _rk; } +private: + H _key; + node* _parent; + node* _left; + node* _right; + int _rk; +}; + +template +class MyMultiBST : public MultiBST { +public: + MyMultiBST() : _root{nullptr} {} + node*& root() { return _root; } + const node*& root() const { return _root; } + int multiplicity(H x) { + auto elem = _search(x); + if(elem) + return elem->rk(); + return 0; + } + MyMultiBST* ins(H x) { + auto iter = _search(x); + if(iter) { + iter->rk() = iter->rk()+1; + } else { + iter = _root; + node* y{nullptr}; + + while(iter) { + y = iter; + if(iter->key() > x) + iter = iter->left(); + else + iter = iter->right(); + } + + node* nodus = new node{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* del(H x) { + node* y; + node* 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* 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* u, node* 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* _max(node* x) { + if(!x) return nullptr; + + auto iter = x; + while(iter->right()) { + iter = iter->right(); + } + + return iter; + } + node* _min(node* x) { + if(!x) return nullptr; + + auto iter = x; + while(iter->left()) { + iter = iter->left(); + } + + return iter; + } + void _inorder(node* p) { + if(p) { + _inorder(p->left()); + for(int i = 0; i < p->rk(); ++i) { + cout << p->key() << ' '; + } + _inorder(p->right()); + } + } + node* _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* _root; +}; + +int main() { + MyMultiBST* t = new MyMultiBST{}; + + 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/Year_1/Programming_2/exercises/exam_20_07_20/ex1.cpp b/Year_1/Programming_2/exercises/exam_20_07_20/ex1.cpp new file mode 100644 index 0000000..4f08a74 --- /dev/null +++ b/Year_1/Programming_2/exercises/exam_20_07_20/ex1.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +using namespace std; +int insertionsort(vector& 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 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/Year_1/Programming_2/exercises/exam_20_07_20/ex2.cpp b/Year_1/Programming_2/exercises/exam_20_07_20/ex2.cpp new file mode 100644 index 0000000..aed25e4 --- /dev/null +++ b/Year_1/Programming_2/exercises/exam_20_07_20/ex2.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +using namespace std; +using pi = tuple>; + +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> v; + priority_queue, comp> pq; + int k; + for(int i = 0; i < R; ++i) { + v.push_back(vector{}); + 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/Year_1/Programming_2/exercises/exam_20_07_20/ex3.cpp b/Year_1/Programming_2/exercises/exam_20_07_20/ex3.cpp new file mode 100644 index 0000000..71f9c65 --- /dev/null +++ b/Year_1/Programming_2/exercises/exam_20_07_20/ex3.cpp @@ -0,0 +1,284 @@ +#include +#include +#include + +using namespace std; + +template +struct node { + T key; + node* prev; + node* right; + node* left; +}; + +template +class bst { +public: + bst() : root{nullptr} , _val{0}{} + + ~bst() { + // TODO + } + + bst* insert(initializer_list&& list) { + for(auto const& i : list) + insert(i); + + return this; + } + + bst* insert(T k) { + node* nodus = new node{k, nullptr, nullptr, nullptr}; + node* iter = root; + node* 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* remove(initializer_list&& list) { + for(auto const& i : list) + remove(i); + + return this; + } + + bst* remove(T k) { + node* nodus = search(k); + if(!nodus) return this; + + if(!nodus->left) { + _transplant(nodus, nodus->right); + } else if(!nodus->right) { + _transplant(nodus, nodus->left); + } else { + node* 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* min() { + return _min(root); + } + + node* min(node* nodus) { + return _min(nodus); + } + + node* max() { + return _max(root); + } + + node* max(node* nodus) { + return _max(nodus); + } + + node* search(T k) { + node* iter = root; + while(iter && iter->key != k) + iter = (iter->key > k) ? iter->left : iter->right; + + return iter; + } + + node* successor(T k) { + node* nodus = search(k); + if(!nodus) return nullptr; + + if(nodus->right) + return min(nodus->right); + + node* prev = nodus->prev; + while(prev && nodus == prev->right) { + nodus = prev; + prev = prev->prev; + } + + return prev; + + } + node* predecessor(T k) { + node* nodus = search(k); + if(!nodus) return nullptr; + + if(nodus->left) + return max(nodus->left); + + node* prev = nodus->prev; + while(prev && nodus == prev->left) { + nodus = prev; + prev = prev->prev; + } + + return prev; + } + + friend ostream& operator<<(ostream& os, bst* tree) { + tree->_inorder(os, tree->root); + return os; + } + T sol(T k) { + return _max(search(k))->key; + } +private: + int _val; + void _transplant(node* u, node* 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* _min(node* root) { + node* iter = root; + while(iter && iter->left) + iter = iter->left; + + return iter; + } + + node* _max(node* root) { + node* iter = root; + while(iter && iter->right) + iter = iter->right; + + return iter; + } + + void _inorder(ostream& os, node* 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* root) { + if(root) { + os << root->key << ' '; + _inorder(os, root->left); + _inorder(os, root->right); + } + } + + void _postorder(ostream& os, node* root) { + if(root) { + _inorder(os, root->left); + _inorder(os, root->right); + os << root->key << ' '; + } + } + node* 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* b = new bst{}; + 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* b = new bst{}; + 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/Year_1/Programming_2/exercises/exam_20_07_20/ex4.cpp b/Year_1/Programming_2/exercises/exam_20_07_20/ex4.cpp new file mode 100644 index 0000000..e234363 --- /dev/null +++ b/Year_1/Programming_2/exercises/exam_20_07_20/ex4.cpp @@ -0,0 +1,293 @@ +#include +#include +#include + +using namespace std; + +template +struct node { + T key; + node* prev; + node* right; + node* left; +}; + +template +class bst { +public: + bst() : root{nullptr} , _val{0}{} + + ~bst() { + // TODO + } + + bst* insert(initializer_list&& list) { + for(auto const& i : list) + insert(i); + + return this; + } + + bst* insert(T k) { + node* nodus = new node{k, nullptr, nullptr, nullptr}; + node* iter = root; + node* 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* remove(initializer_list&& list) { + for(auto const& i : list) + remove(i); + + return this; + } + + bst* remove(T k) { + node* nodus = search(k); + if(!nodus) return this; + + if(!nodus->left) { + _transplant(nodus, nodus->right); + } else if(!nodus->right) { + _transplant(nodus, nodus->left); + } else { + node* 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* min() { + return _min(root); + } + + node* min(node* nodus) { + return _min(nodus); + } + + node* max() { + return _max(root); + } + + node* max(node* nodus) { + return _max(nodus); + } + + node* search(T k) { + node* iter = root; + while(iter && iter->key != k) + iter = (iter->key > k) ? iter->left : iter->right; + + return iter; + } + + node* successor(T k) { + node* nodus = search(k); + if(!nodus) return nullptr; + + if(nodus->right) + return min(nodus->right); + + node* prev = nodus->prev; + while(prev && nodus == prev->right) { + nodus = prev; + prev = prev->prev; + } + + return prev; + + } + node* predecessor(T k) { + node* nodus = search(k); + if(!nodus) return nullptr; + + if(nodus->left) + return max(nodus->left); + + node* prev = nodus->prev; + while(prev && nodus == prev->left) { + nodus = prev; + prev = prev->prev; + } + + return prev; + } + + friend ostream& operator<<(ostream& os, bst* 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* u, node* 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* _min(node* root) { + node* iter = root; + while(iter && iter->left) + iter = iter->left; + + return iter; + } + + node* _max(node* root) { + node* iter = root; + while(iter && iter->right) + iter = iter->right; + + return iter; + } + + void _inorder(ostream& os, node* 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* root) { + if(root) { + os << root->key << ' '; + _inorder(os, root->left); + _inorder(os, root->right); + } + } + + void _postorder(ostream& os, node* root) { + if(root) { + _inorder(os, root->left); + _inorder(os, root->right); + os << root->key << ' '; + } + } + node* 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* b = new bst{}; + 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* b = new bst{}; + 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/Year_1/Programming_2/exercises/exam_20_07_20/ex5.cpp b/Year_1/Programming_2/exercises/exam_20_07_20/ex5.cpp new file mode 100644 index 0000000..dd8fc98 --- /dev/null +++ b/Year_1/Programming_2/exercises/exam_20_07_20/ex5.cpp @@ -0,0 +1,285 @@ +#include +#include +#include +#include +#include +#include +#define INF 99999999 +#define W -1 +#define G 0 +#define B 1 + +using namespace std; + +template +class graph { +public: + graph(int len) : _len{len}, _nodes{0}, _edges{0} { + _k = new H*[len]; + _adj = new vector[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* add_node(H x) { + if(_index(x) > -1) return this; + if(_nodes == _len) return this; + _k[_nodes++] = new H(x); + + return this; + } + graph* 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 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* _transpose() { + graph* g = new graph{_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 _stack; + H** _k; + vector* _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* g = new graph{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()<* g = new graph{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()<* g = new graph{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()< +#include +#include + +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/Year_1/Programming_2/exercises/inserimento-coda.cc b/Year_1/Programming_2/exercises/inserimento-coda.cc new file mode 100644 index 0000000..6775ca2 --- /dev/null +++ b/Year_1/Programming_2/exercises/inserimento-coda.cc @@ -0,0 +1,119 @@ +#include +#include + +using namespace std; + +template +class node { +public: + node(T key, node* next) : _key{key}, _next{next} {} + node(T key) : _key{key}, _next{nullptr} {} + const T& key() const { return _key; } + T& key() { return _key; } + const node*& next() const { return _next; } + node*& next() { return _next; } +private: + T _key; + node* _next; +}; + +template +class Coda { +public: + Coda() : _head{nullptr}, _tail{nullptr} {} + ~Coda() { + + } + Coda* enqueue(T val) { + if(!_head) { + _head = new node{val, nullptr}; + _tail = _head; + } else { + _tail->next() = new node{val, nullptr}; + _tail = _tail->next(); + } + + return this; + } + node* pop() { + if(!_head) return nullptr; + node* elem = _head; + delete _head; + _head = elem->next(); + + return elem; + } + + bool is_empty() { return _head == nullptr; } +private: + node* _head; + node* _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* queue = new Coda{}; + 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* queue = new Coda{}; + 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* queue = new Coda{}; + 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* queue = new Coda{}; + 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/Year_1/Programming_2/exercises/inserimento-pila.cc b/Year_1/Programming_2/exercises/inserimento-pila.cc new file mode 100644 index 0000000..0dbe99b --- /dev/null +++ b/Year_1/Programming_2/exercises/inserimento-pila.cc @@ -0,0 +1,116 @@ +#include +#include + +using namespace std; + +template +class node { +public: + node(T key, node* next) : _key{key}, _next{next} {} + node(T key) : _key{key}, _next{nullptr} {} + const T& key() const { return _key; } + T& key() { return _key; } + const node*& next() const { return _next; } + node*& next() { return _next; } +private: + T _key; + node* _next; +}; + +template +class Pila { +public: + Pila() : _head{nullptr}{} + ~Pila() { + + } + Pila* push(T val) { + if(!_head) { + _head = new node{val, nullptr}; + } else { + _head = new node{val, _head}; + } + + return this; + } + node* pop() { + if(!_head) return nullptr; + node* elem = _head; + delete _head; + _head = elem->next(); + + return elem; + } + + bool is_empty() { return _head == nullptr; } +private: + node* _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* stack = new Pila{}; + 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* stack = new Pila{}; + 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* stack = new Pila{}; + 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* stack = new Pila{}; + 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/Year_1/Programming_2/exercises/matrice-adj.cc b/Year_1/Programming_2/exercises/matrice-adj.cc new file mode 100644 index 0000000..7830f97 --- /dev/null +++ b/Year_1/Programming_2/exercises/matrice-adj.cc @@ -0,0 +1,151 @@ +#include +#include +#include +#include + +using namespace std; + +template +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* 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* 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> print() { + vector> v; + for(int i = 0; i < _len; ++i) { + v.push_back(vector{*_k[i]}); + vector 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 +string result(vector> 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* g = new matrix_graph(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* g = new matrix_graph(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* g = new matrix_graph(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/Year_1/Programming_2/exercises/minore.cc b/Year_1/Programming_2/exercises/minore.cc new file mode 100644 index 0000000..677da76 --- /dev/null +++ b/Year_1/Programming_2/exercises/minore.cc @@ -0,0 +1,27 @@ +#include +#include +#include + +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/Year_1/Programming_2/exercises/pop-stack.cc b/Year_1/Programming_2/exercises/pop-stack.cc new file mode 100644 index 0000000..3739450 --- /dev/null +++ b/Year_1/Programming_2/exercises/pop-stack.cc @@ -0,0 +1,132 @@ +#include +#include + +using namespace std; + +template +class node { +public: + node(T key, node* next) : _key{key}, _next{next} {} + node(T key) : _key{key}, _next{nullptr} {} + const T& key() const { return _key; } + T& key() { return _key; } + const node*& next() const { return _next; } + node*& next() { return _next; } +private: + T _key; + node* _next; +}; + +template +class Pila { +public: + Pila() : _head{nullptr}{} + ~Pila() { + + } + Pila* push(T val) { + if(!_head) { + _head = new node{val, nullptr}; + } else { + _head = new node{val, _head}; + } + + return this; + } + node* pop() { + if(!_head) return nullptr; + node* elem = _head; + delete _head; + _head = elem->next(); + + return elem; + } + + bool is_empty() { return _head == nullptr; } +private: + node* _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* stack = new Pila{}; + 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* stack = new Pila{}; + 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* stack = new Pila{}; + 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* stack = new Pila{}; + 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/Year_1/Programming_2/exercises/prima-maiuscola.cc b/Year_1/Programming_2/exercises/prima-maiuscola.cc new file mode 100644 index 0000000..6273219 --- /dev/null +++ b/Year_1/Programming_2/exercises/prima-maiuscola.cc @@ -0,0 +1,30 @@ +#include +#include +#include + +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/Year_1/Programming_2/exercises/ripetute.cc b/Year_1/Programming_2/exercises/ripetute.cc new file mode 100644 index 0000000..23d151a --- /dev/null +++ b/Year_1/Programming_2/exercises/ripetute.cc @@ -0,0 +1,20 @@ +#include +#include +#include + +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/Year_1/Programming_2/exercises/sol-ripetute.cc b/Year_1/Programming_2/exercises/sol-ripetute.cc new file mode 100644 index 0000000..1518d76 --- /dev/null +++ b/Year_1/Programming_2/exercises/sol-ripetute.cc @@ -0,0 +1,25 @@ +#include +#include +#include + +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/Year_1/Programming_2/exercises/sottosequenza.cc b/Year_1/Programming_2/exercises/sottosequenza.cc new file mode 100644 index 0000000..1d7c481 --- /dev/null +++ b/Year_1/Programming_2/exercises/sottosequenza.cc @@ -0,0 +1,26 @@ +#include +#include + +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/Year_1/Programming_2/exercises/stringa-inversa.cc b/Year_1/Programming_2/exercises/stringa-inversa.cc new file mode 100644 index 0000000..5c37718 --- /dev/null +++ b/Year_1/Programming_2/exercises/stringa-inversa.cc @@ -0,0 +1,26 @@ +#include +#include + +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(); +} -- cgit v1.2.3-18-g5258