diff --git a/chessboard.cpp b/chessboard.cpp new file mode 100644 index 0000000..5a4acc3 --- /dev/null +++ b/chessboard.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +using namespace std; + +class ChessBoardArray { + protected: + class Row { + public: + Row(ChessBoardArray &a, int i):chessBoardArray(a), row(i) {} + int & operator [] (int i) const{ + return chessBoardArray.select(row, i); + } + + private: + ChessBoardArray &chessBoardArray; + int row; + }; + class ConstRow { + public: + ConstRow(const ChessBoardArray &a, int i) : chessBoardArray(a), row(i) {}; + int operator [] (int i) const { + return chessBoardArray.select(row, i); + } + + private: + const ChessBoardArray &chessBoardArray; + int row; + }; + + public: + ChessBoardArray(unsigned size=0, unsigned base=0) : data(new int[(size*size+1)/2]), thebase(base), thesize(size) {} + + ChessBoardArray(const ChessBoardArray &a) : data(new int[(a.thesize*a.thesize+1)/2]), thebase(a.thebase), thesize(a.thesize) { + for(unsigned i=0; i<((thesize*thesize+1)/2); ++i) data[i]=a.data[i]; + } + + ~ChessBoardArray() {delete [] data;} + + ChessBoardArray & operator = (const ChessBoardArray &a) { + delete [] data; + thebase=a.thebase; + thesize=a.thesize; + data=new int[(thesize*thesize+1)/2]; + for(unsigned i=0; i<((thesize*thesize+1)/2); ++i) data[i]=a.data[i]; + return *this; + } + + int & select(int i, int j) { + return data[loc(i,j)]; + } + int select(int i, int j) const { + return data[loc(i,j)]; + } + + const Row operator [] (int i) { + return Row(*this, i); + } + const ConstRow operator [] (int i) const { + return ConstRow(*this, i); + } + + friend ostream & operator << (ostream &out, const ChessBoardArray &a) { + for(int line=0; line=thesize || dj<0 || dj>=thesize || (i+j)%2==1) throw out_of_range("invalid index"); + return (di*thesize +dj)/2; + } + +}; + + + diff --git a/cycle.cpp b/cycle.cpp new file mode 100644 index 0000000..449496c --- /dev/null +++ b/cycle.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +using namespace std; + +class Graph{ + private: + int numofvertices; + list *ptr; + vector color; + vector root; + int startofcircle, endofcircle; + + bool DFS(int v) { + color[v]=1; + for(int u: ptr[v]){ + if(color[u]==0) { + root[u]=v; + if(DFS(u)) + return true; + } + else if(color[u]==1) { + endofcircle=v; + startofcircle=u; + return true; + } + } + color[v]=2; + return false; + } + + + public: + + Graph(int V): numofvertices(V) { ptr=new list[numofvertices]; } + + ~Graph() { delete [] ptr;} + + void addEdge (int u, int v) { + ptr[u].push_back(v); + } + + bool cycle(vector &cycle) { + color.assign(numofvertices, 0); + root.assign(numofvertices, -1); + startofcircle=-1; + + for(int v=0; v +#include +#include +using namespace std; + + +int main() { + + + int N; + int M; + int u, v; + int per=0; + int pathpart[4]; + + cin>>N; + cin>>M; + + int nodevalue[N]; + + for (int i=0; i>u; + cin>>v; + nodevalue[u]++; + nodevalue[v]++; + } + for (int j=0; j2){ + cout<<"IMPOSSIBLE"; + cout< +#ifndef CONTEST +#include "fullratio.hpp" +#endif +using namespace std; + +std::ostream& operator << (std::ostream &out, const rational &x) { + out<0 && b>0) { + if(a>b) a=a%b; + else b=b%a; + } + return a+b; +} + +rational::rational (int n, int d) { + int mkd=gcd(n, d); + nom=n/mkd; + den=d/mkd; + if(den<0){ + nom=(-1*nom); + den=(-1*den); + } +} + +rational operator + (const rational &x, const rational &y) { + int arithm=((x.nom)*(y.den))+((y.nom)*(x.den)); + int parono=((x.den)*(y.den)); + + int a=arithm; + int b=parono; + if(a<0) a=(-1)*a; + if(b<0) b=(-1)*b; + + int c=rational::gcd(a,b); + return ( rational(arithm/c, parono/c) ); +} + + +rational operator - (const rational &x, const rational &y) { + int arithm=((x.nom)*(y.den))-((y.nom)*(x.den)); + int parono=((x.den)*(y.den)); + + int a=arithm; + int b=parono; + if(a<0) a=(-1)*a; + if(b<0) b=(-1)*b; + + int c=rational::gcd(a,b); + + return ( rational(arithm/c, parono/c) ); +} + +rational operator * (const rational &x, const rational &y) { + int arithm=((x.nom)*(y.nom)); + int parono=((x.den)*(y.den)); + int a=arithm; + int b=parono; + if(a<0) a=(-1)*a; + if(b<0) b=(-1)*b; + + int c=rational::gcd(a,b); + + return ( rational(arithm/c, parono/c) ); +} + +rational operator / (const rational &x, const rational &y) { + int arithm=((x.nom)*(y.den)); + int parono=((x.den)*(y.nom)); + int a=arithm; + int b=parono; + if(a<0) a=(-1)*a; + if(b<0) b=(-1)*b; + + int c=rational::gcd(a,b); + + return ( rational(arithm/c, parono/c) ); +} + diff --git a/lexicon.cpp b/lexicon.cpp new file mode 100644 index 0000000..9b0c63d --- /dev/null +++ b/lexicon.cpp @@ -0,0 +1,212 @@ + + +class lexicon +{ + private: + class Node + { + public: + std::string key; + Node* left=nullptr; + Node* right=nullptr; + int freq=1; + }; + + Node* root; + public: + + lexicon():root(nullptr){} + + ~lexicon(){purge(root);} + + void purge(Node* root) + { + if(root==nullptr) + return; + purge(root->left); + purge(root->right); + delete root; + } + Node* createNode(const std::string &s) + { + Node* newNode = new Node(); + newNode->key=s; + newNode->left=newNode->right=nullptr; + newNode->freq = 1; + return newNode; + } + + void insert(const std::string &s) + { + if(root==nullptr) + { + root=createNode(s); + }else if(insert(root,s)){} + } + bool insert(Node* t, const std::string &s) + { + if(skey) + { + if(t->left==nullptr) + { + t->left=createNode(s); + return true; + }else + return insert(t->left,s); + }else if(s>t->key) + { + if(t->right==nullptr) + { + t->right=createNode(s); + return true; + }else + return insert(t->right,s); + }else + { + ++(t->freq); + return false; + } + } + int lookup(const std::string &s) const + { + return lookup(root,s); + } + Node* minVal(Node* root) + { + Node* temp = root; + while(temp->left!=nullptr) + temp= temp->left; + return temp; + } + void remove(const std::string & s) + { + root=remove(root,s); + } + Node* remove(Node* root, const std::string &s) + { + if(root==nullptr) + return root; + if(skey) + root->left=remove(root->left,s); + else if(s>root->key) + root->right=remove(root->right,s); + else + { + if(root->left==nullptr) + { + Node* temp= root->right; + delete root; + return temp; + } + if(root->right==nullptr) + { + Node* temp = root->left; + delete root; + return temp; + } + Node* temp = minVal(root->left); + root->key=temp->key; + root->freq=temp->freq; + root->left=remove(root->left,temp->key); + } + return root; + } + + int lookup(Node* root, const std::string& s) const + { + + if(root==nullptr) + return 0; + if(s==root->key) + return root->freq; + else if(skey) + return lookup(root->left,s); + else if(s>root->key) + return lookup(root->right,s); + } + + void Inorder()const + { + Inorder(root); + } + void Inorder(Node* root)const + { + if(root==nullptr) + return; + Inorder(root->left); + cout<key<<" "<freq<right); + } + + void replace(const string &s1, const string &s2) + { + + if(lookup(s1)==0) + return; + else + { + int s1_freq=0; + Node*node_1=search_string(s1); + s1_freq=node_1->freq; + remove(s1); + if(lookup(s2)==0) + { + insert(s2); + + search_string(s2)->freq=s1_freq; + }else + search_string(s2)->freq+=s1_freq; + } + + } + friend ostream& operator<<(ostream& stream, const lexicon& l) + { + l.Inorder(l.root); + stream<<""; + return stream; + } + Node* search_string(const std::string &s) + { + return search_string(root,s); + } + Node* search_string(Node* root,const std::string &s) + { + if(root==nullptr || s==root->key) + return root; + else if(skey) + return search_string(root->left,s); + else if(s>root->key) + return search_string(root->right,s); + } +int getLevelUtil(Node* root ,const string& data, int level) const +{ + if (root == nullptr) + return 0; + if (root -> key == data) + return level; + int downlevel = getLevelUtil(root -> left, + data, level + 1); + if (downlevel != 0) + return downlevel; + downlevel = getLevelUtil(root->right, + data, level + 1); + return downlevel; +} +int depth( const string &s) const +{ + return getLevelUtil(root, s, 1); +} +void print() +{ + cout<key< +#include +#include +#include +#include +#include +using namespace std; diff --git a/polynomial.cpp b/polynomial.cpp new file mode 100644 index 0000000..7e95502 --- /dev/null +++ b/polynomial.cpp @@ -0,0 +1,254 @@ +#include +using namespace std; + +class Polynomial { + public: + class Term { + public: + int exponent; + int coefficient; + Term *next; + protected: + Term(int exp, int coeff, Term *n) : exponent(exp), coefficient(coeff), next(n){}; + friend class Polynomial; + }; + + Term *head; + int degree; + int size; + + Polynomial():head(nullptr), degree(0), size(0) {} + Polynomial(const Polynomial &p): head(nullptr), degree(0), size(0) {copy(p);}; + ~Polynomial() {purge();} + + Polynomial & operator =(const Polynomial &p) {clear(); copy(p); return *this;} + + void purge() { + Term *l=head; + degree=0; + size=0; + while(l!=nullptr) { + Term *q=l; + l=l->next; + delete q; + } + } + + void clear() { + purge(); + head=nullptr; + degree=0; + size=0; + } + + void remove(int exp) /* AFAIREI STOIXEIO*/ { + Term* temp=head; + Term* prev; + if(head==nullptr) return; + else { + if(head->exponent==exp) { + head=head->next; + delete temp; + } + else { + for(temp = head;temp->next!=nullptr && temp->exponent !=exp;temp = temp->next) { + prev=temp; + } + if(temp->exponent==exp) { + prev->next = temp->next; + delete temp; + } + else return; + } + --size; + } + } + + + + void addTerm(int expon, int coeff) { + if(coeff==0 || expon<0) return; + Term *newTerm = new Term(expon,coeff,nullptr); + Term *temp = head; + if(expon>degree) degree=expon; + if(head==nullptr) { + newTerm->next=nullptr; + head=newTerm; + ++size; + return; + } + if(expon>head->exponent) { + newTerm->next=head; + head=newTerm; + ++size; + return; + } + if(expon==head->exponent) /* HERE */ { + head->coefficient=head->coefficient+newTerm->coefficient; + if(head->coefficient==0) remove(head->exponent); + return; + } + while(temp->next!=nullptr) { + if(expon==temp->exponent) /*here*/ { + temp->coefficient=temp->coefficient+newTerm->coefficient; + if(temp->coefficient==0) + remove(temp->exponent); + return; + } + if(expon>temp->next->exponent) { + newTerm->next = temp->next; + temp->next = newTerm; + return; + } + temp = temp->next; + } + if(newTerm->exponent==temp->exponent) /*HERE*/ { + temp->coefficient=temp->coefficient + newTerm->coefficient; + if(temp->coefficient==0) remove(temp->exponent); + return; + } + else { + newTerm->next=nullptr; + temp->next=newTerm; + //temp->coefficient=temp->coefficient+newTerm->coefficient; + ++size; + } + return; + } + + double evaluate(double x) { + double sum=0; + int exp; + for(Term *l=head; l!=nullptr; l=l->next) { + exp=l->exponent; + double expvalue=1; + if (exp==0) expvalue=1; + else { + for(int i=0; icoefficient)*expvalue; + } + return sum; + } + + friend Polynomial operator+ (const Polynomial &p, const Polynomial &q) { + Polynomial r; + Polynomial::Term *temp_p = p.head; + Polynomial::Term *temp_q = q.head; + while(!(temp_p==nullptr && temp_q ==nullptr)) { + if(temp_p==nullptr && temp_q!=nullptr) { + r.addTerm(temp_q->exponent,temp_q->coefficient); + temp_q=temp_q->next; + continue; + } + if(temp_q==nullptr && temp_p!=nullptr) { + r.addTerm(temp_p->exponent,temp_p->coefficient); + temp_p=temp_p->next; + continue; + } + if(temp_p->exponent == temp_q->exponent) { + r.addTerm(temp_p->exponent,temp_p->coefficient + temp_q ->coefficient); + temp_p=temp_p->next; + temp_q=temp_q->next; + } + else if(temp_p->exponent > temp_q ->exponent ) { + r.addTerm(temp_p->exponent, temp_p->coefficient); + temp_p=temp_p->next; + } + else if(temp_p->exponent < temp_q->exponent) { + r.addTerm(temp_q->exponent,temp_q->coefficient); + temp_q = temp_q->next; + } + } + return r; +} + friend Polynomial operator* (const Polynomial &p, const Polynomial &q) { + Polynomial r[p.size+q.size+30]; + Polynomial product; + int counter=0; + for(Polynomial :: Term *i = p.head; i!=nullptr; i = i->next) { + for(Polynomial :: Term * j= q.head; j!=nullptr; j = j->next) { + r[counter].addTerm(i->exponent+j->exponent, (i->coefficient)*(j->coefficient)); + } + //cout<next) { + if(i==p.head) { + if(i->exponent==0) { + if(i->coefficient < 0) out<<"- "<<-(i->coefficient); + else if(i->coefficient > 0) out<coefficient; + } + else if(i->exponent==1) { + if(i->coefficient < 0) { + if(i->coefficient==-1) out<<"- "<<"x"; + else out<<"- "<<-(i->coefficient)<<"x"; + } + else if(i->coefficient > 0) { + if(i->coefficient==1) out<<"x"; + else out<coefficient<<"x"; + } + } + else if(i->exponent > 1) { + if(i->coefficient < 0) { + if(i->coefficient==-1) out<<"- "<<"x^"<exponent; + else out<<"- "<<-(i->coefficient)<<"x^"<exponent; + } + else if(i->coefficient > 0) { + if(i->coefficient==1) out<<"x^"<exponent; + else out<coefficient<<"x^"<exponent; + } + } + } + else { + if(i->exponent==0) { + if(i->coefficient < 0) out<<" - "<<-(i->coefficient); + else if(i->coefficient > 0) out<<" + "<coefficient; + } + else if(i->exponent==1) { + if(i->coefficient < 0) { + if(i->coefficient==-1) out<<" - "<<"x"; + else out<<" - "<<-(i->coefficient)<<"x"; + } + else if(i->coefficient > 0) { + if(i->coefficient==1) out<<" + "<<"x"; + else out<<" + "<coefficient<<"x"; + } + } + else if(i->exponent > 1) { + if(i->coefficient < 0) { + if(i->coefficient==-1) out<<" - "<<"x^"<exponent; + else out<<" - "<<-(i->coefficient)<<"x^"<exponent; + } + else if(i->coefficient > 0) { + if(i->coefficient==1) out<<" + "<<"x^"<exponent; + else out<<" + "<coefficient<<"x^"<exponent; + } + } + } + } + //out<<"\n"; + return out; + } + + private: + void copy(const Polynomial &p) { + for(Term *l=p.head; l!=nullptr; l=l->next) addTerm(l->exponent, l->coefficient); + } +}; + +