problemscpp
A collection of my answers to algorithm problems in c++.
载入中...
搜索中...
未找到
templates.h
浏览该文件的文档.
1#pragma once
2#include <array>
3#include <string>
4#include <vector>
5
6using namespace std;
7
9struct TrieNode {
10 char ch;
11 array<TrieNode *, 26> nexts = {};
12 bool end_of_word = false;
13 unsigned count = 0;
14
15 explicit TrieNode(char ch)
16 : ch(ch) {}
17
18 void insert(const string &str);
19 ~TrieNode();
20};
21
23class BigInt {
24private:
25 bool positive = true;
26 vector<unsigned short> vec = {};
27 [[nodiscard]] unsigned long get_size() const;
28 unsigned short operator[](unsigned long /*i*/) const;
29 BigInt(const vector<unsigned short> &vec, bool positive);
30 vector<unsigned short> operator*(unsigned short n) const;
31
32public:
33 BigInt(short n);
34 BigInt(int n);
35 BigInt(long n);
36 BigInt(long long n);
37 BigInt(unsigned short n);
38 BigInt(unsigned int n);
39 BigInt(unsigned long n);
40 BigInt(unsigned long long n);
41 BigInt(const string &str);
42 BigInt(const char *str);
43 BigInt(const BigInt &bi);
44 BigInt operator+(const BigInt &bi) const;
45 BigInt operator-(const BigInt &bi) const;
46 BigInt operator*(const BigInt &bi) const;
47 BigInt operator/(const BigInt &bi) const;
48 BigInt operator%(const BigInt &bi) const;
49 BigInt operator-() const;
50 BigInt &operator+=(const BigInt &bi);
51 BigInt &operator-=(const BigInt &bi);
52 BigInt &operator*=(const BigInt &bi);
53 BigInt &operator/=(const BigInt &bi);
54 BigInt &operator%=(const BigInt &bi);
57 BigInt operator++(int);
58 BigInt operator--(int);
59 bool operator>(const BigInt &bi) const;
60 bool operator<(const BigInt &bi) const;
61 bool operator==(const BigInt &bi) const;
62 bool operator!=(const BigInt &bi) const;
63 bool operator>=(const BigInt &bi) const;
64 bool operator<=(const BigInt &bi) const;
65 friend ostream &operator<<(ostream &os, const BigInt & /*bi*/);
66 friend istream &operator>>(istream &is, const BigInt & /*bi*/);
67 BigInt();
68};
69
71class Fraction {
72private:
73 bool positive;
74 unsigned long long numerator;
75 unsigned long long denominator;
76 void simplify();
77
78public:
79 Fraction(bool positive, long long numerator, long long denominator);
80 Fraction operator+(const Fraction &f) const;
81 Fraction operator-(const Fraction &f) const;
82 Fraction operator*(const Fraction &f) const;
83 Fraction operator/(const Fraction &f) const;
84 [[nodiscard]] bool is_positive() const;
85 [[nodiscard]] unsigned long long get_numerator() const;
86 [[nodiscard]] unsigned long long get_denominator() const;
87 friend ostream &operator<<(ostream &os, const Fraction &frac);
88};
89
91class UnionFind {
92private:
93 vector<int> parent;
94 vector<int> rank;
95 vector<int> size;
96
97public:
98 explicit UnionFind(int n);
99 int find(int x);
100 void unite(int x, int y);
101 bool same(int x, int y);
102 int get_size(int x);
103 unsigned count();
104};
105
107class Matrix {
108private:
109 vector<vector<int>> mat;
110
111public:
112 Matrix(int n);
113 Matrix(const Matrix &m);
114 Matrix operator*(const Matrix &m) const;
115 vector<int> &operator[](int i);
116 const vector<int> &operator[](int i) const;
117 static Matrix identity(int n);
118};
array< TrieNode *, 26 > nexts
unsigned count
void insert(const string &str)
TrieNode(char ch)
BigInt(const vector< unsigned short > &vec, bool positive)
BigInt & operator++()
bool operator>(const BigInt &bi) const
BigInt operator+(const BigInt &bi) const
vector< unsigned short > operator*(unsigned short n) const
unsigned short operator[](unsigned long) const
unsigned long get_size() const
friend ostream & operator<<(ostream &os, const BigInt &)
BigInt & operator*=(const BigInt &bi)
bool operator<(const BigInt &bi) const
BigInt & operator-=(const BigInt &bi)
bool operator!=(const BigInt &bi) const
BigInt operator-() const
bool positive
BigInt(unsigned long long n)
BigInt operator/(const BigInt &bi) const
BigInt operator%(const BigInt &bi) const
bool operator<=(const BigInt &bi) const
bool operator>=(const BigInt &bi) const
vector< unsigned short > vec
BigInt(long long n)
friend istream & operator>>(istream &is, const BigInt &)
BigInt & operator/=(const BigInt &bi)
BigInt & operator+=(const BigInt &bi)
BigInt & operator--()
bool operator==(const BigInt &bi) const
BigInt & operator%=(const BigInt &bi)
bool is_positive() const
Fraction operator+(const Fraction &f) const
Fraction(bool positive, long long numerator, long long denominator)
Fraction operator-(const Fraction &f) const
unsigned long long get_denominator() const
unsigned long long get_numerator() const
unsigned long long denominator
分母
bool positive
正负
Fraction operator/(const Fraction &f) const
unsigned long long numerator
分子
friend ostream & operator<<(ostream &os, const Fraction &frac)
Fraction operator*(const Fraction &f) const
vector< int > size
void unite(int x, int y)
vector< int > rank
int get_size(int x)
vector< int > parent
bool same(int x, int y)
int find(int x)
unsigned count()
vector< vector< int > > mat
Matrix operator*(const Matrix &m) const
vector< int > & operator[](int i)
static Matrix identity(int n)