Add files via upload

This commit is contained in:
nickarafyllis 2020-07-15 01:25:53 +03:00 committed by GitHub
parent 180384307f
commit b6a9a7577f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 781 additions and 0 deletions

91
chessboard.cpp Normal file
View file

@ -0,0 +1,91 @@
#include <iostream>
#include <iomanip>
#include <stdexcept>
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<a.thesize; ++line) {
for(int sthlh=0; sthlh<a.thesize; ++sthlh) {
cout<<setw(4);
if((line+sthlh)%2==0)
cout<<a.select(line+a.thebase,sthlh+a.thebase);
else cout<<0;
}
cout<<endl;
}
return out;
}
protected:
int *data;
int thebase;
unsigned thesize;
unsigned int loc(int i, int j) const throw(out_of_range) {
int di=i-thebase, dj=j-thebase;
if(di<0 || di>=thesize || dj<0 || dj>=thesize || (i+j)%2==1) throw out_of_range("invalid index");
return (di*thesize +dj)/2;
}
};

66
cycle.cpp Normal file
View file

@ -0,0 +1,66 @@
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;
class Graph{
private:
int numofvertices;
list<int> *ptr;
vector<int> color;
vector<int> 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<int>[numofvertices]; }
~Graph() { delete [] ptr;}
void addEdge (int u, int v) {
ptr[u].push_back(v);
}
bool cycle(vector<int> &cycle) {
color.assign(numofvertices, 0);
root.assign(numofvertices, -1);
startofcircle=-1;
for(int v=0; v<numofvertices; v++) {
if(color[v]==0 && DFS(v))
break;
}
if(startofcircle==-1)
return false;
else {
cycle.push_back(startofcircle);
for( int v=endofcircle; v!=startofcircle; v=root[v])
cycle.push_back(v);
reverse(cycle.begin(), cycle.end());
return true;
}
}
};

71
euler.cpp Normal file
View file

@ -0,0 +1,71 @@
#include <iostream>
#include <string>
#include <array>
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<N; i++) {
nodevalue[i]=0;
}
for(int i=0; i<M; i++) {
cin>>u;
cin>>v;
nodevalue[u]++;
nodevalue[v]++;
}
for (int j=0; j<N; j++) {
if (nodevalue[j]%2!=0) {
pathpart[per]=j;
per++;
if (per>2){
cout<<"IMPOSSIBLE";
cout<<endl;
exit(0);
}
}
}
if (per==0) {
cout<< "CYCLE";
cout<<endl;
}
else if (per==2) {
cout<< "PATH ";
if (pathpart[0]<pathpart[1]) {
cout<<pathpart[0];
cout<<" ";
cout<<pathpart[1];
cout<<endl;
}
else {
cout<<pathpart[1];
cout<<" ";
cout<<pathpart[0];
cout<<endl;
}
}
else {
cout<<"IMPOSSIBLE";
cout<<endl;
}
}

87
fullratio.cpp Normal file
View file

@ -0,0 +1,87 @@
#include <iostream>
#ifndef CONTEST
#include "fullratio.hpp"
#endif
using namespace std;
std::ostream& operator << (std::ostream &out, const rational &x) {
out<<x.nom<<"/"<<x.den;
return out;
}
int rational::gcd(int a, int b) {
if(a<0) a=(-1)*a;
if(b<0) b=(-1)*b;
while(a>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) );
}

212
lexicon.cpp Normal file
View file

@ -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(s<t->key)
{
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(s<root->key)
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(s<root->key)
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<<root->key<<" "<<root->freq<<endl;
Inorder(root->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(s<root->key)
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<<root->key<<endl;
}
};
#include <iostream>
#include <string>
#include <list>
#include <array>
#include <vector>
#include <algorithm>
using namespace std;

254
polynomial.cpp Normal file
View file

@ -0,0 +1,254 @@
#include <iostream>
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; i<exp; ++i) {
expvalue*=x;
}
}
sum+=(l->coefficient)*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<<r[counter];
++counter;
}
product = r[0];
for(int i=1;i<counter;i++) {
product = product + r[i];
}
return product;
}
friend ostream & operator << (ostream &out, const Polynomial &p) {
if(p.head==nullptr) {
out<<"0";
return out;
}
for(Polynomial::Term *i = p.head;i!=nullptr;i=i->next) {
if(i==p.head) {
if(i->exponent==0) {
if(i->coefficient < 0) out<<"- "<<-(i->coefficient);
else if(i->coefficient > 0) out<<i->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<<i->coefficient<<"x";
}
}
else if(i->exponent > 1) {
if(i->coefficient < 0) {
if(i->coefficient==-1) out<<"- "<<"x^"<<i->exponent;
else out<<"- "<<-(i->coefficient)<<"x^"<<i->exponent;
}
else if(i->coefficient > 0) {
if(i->coefficient==1) out<<"x^"<<i->exponent;
else out<<i->coefficient<<"x^"<<i->exponent;
}
}
}
else {
if(i->exponent==0) {
if(i->coefficient < 0) out<<" - "<<-(i->coefficient);
else if(i->coefficient > 0) out<<" + "<<i->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<<" + "<<i->coefficient<<"x";
}
}
else if(i->exponent > 1) {
if(i->coefficient < 0) {
if(i->coefficient==-1) out<<" - "<<"x^"<<i->exponent;
else out<<" - "<<-(i->coefficient)<<"x^"<<i->exponent;
}
else if(i->coefficient > 0) {
if(i->coefficient==1) out<<" + "<<"x^"<<i->exponent;
else out<<" + "<<i->coefficient<<"x^"<<i->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);
}
};