SlideShare a Scribd company logo
1 of 68
Download to read offline
3
4
// Polynomial: A set of pairs, <e_i, a_i>
// a_i: coefficient (not 0, float type)
// e_i: exponent (not negative, int type)
class Polynomial
{
public:
// Create polynomial p(x) = 0
Polynomial();
// Add polynomial *this and poly
const Polynomial Add(const Polynomial& poly);
// Multiply polynomial *this and poly
const Polynomial Multiply(const Polynomial& poly);
// After assign f into x, evaluate polynomial and return result
const float Eval(const float& f);
};
5
private:
static const int MaxDegree = 100;
int degree; // degree <= MaxDegree
float coef[MaxDegree + 1]; // Coefficient array
6
private:
int degree;
float* coef; // Coefficient array
Polynomial::Polynomial(int d)
: degree(d), coef(new float[degree + 1]) { }
7
class Polynomial; // Forward Declaration
class Term
{
friend Polynomial;
private:
float coef; // Coefficient
int exp; // Exponent
};
class Polynomial
{
private:
Term* termArray; // Term array (not 0)
int capacity; // Capacity of termArray
int terms; // Number of terms (not 0)
...
};
3 2 -4coef
3 2 1exp
𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥
1 -10 -3 1coef
8 5 3 0exp
𝑏𝑏 𝑥𝑥 = 𝑥𝑥8
− 10𝑥𝑥5
− 3𝑥𝑥3
+ 1
8
const Polynomial Polynomial::Add(const Polynomial& b)
{ // Return *this + b
Polynomial c;
int aPos = 0, bPos = 0;
while ((aPos < terms) && (bPos < b.terms)) {
if (termArray[aPos].exp == b.termArray[bPos].exp) {
float t = termArray[aPos].coef + b.termArray[bPos].coef;
if (t) c.NewTerm(t, termArray[aPos].exp);
aPos++; bPos++;
}
else if (termArray[aPos].exp < b.termArray[bPos].exp) {
c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);
bPos++;
}
else {
c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);
aPos++;
}
}
9
// Add rest terms of *this
for (; aPos < terms; aPos++)
c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);
// Add rest terms of b(x)
for (; bPos < b.terms; bPos++)
c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);
return c;
}
10
void Polynomial::NewTerm(const float& coef, const int& exp)
{ // Add new term into into last element of termArray
if (terms == capacity)
{ // Expand the size of termArray twice
capacity *= 2;
Term* temp = new Term[capacity];// New array
std::copy(termArray, termArray + terms, temp);
delete[] termArray;// Return original array
termArray = temp;
}
termArray[terms].coef = coef;
termArray[terms++].exp = exp;
}
11
Polynomial c;
int aPos = 0, bPos = 0; 3 2 -4coef
3 2 1exp
𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥
1 -10 -3 1coef
8 5 3 0exp
𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1
aPos
bPos
𝑐𝑐 𝑥𝑥 = 𝑥𝑥8
− 10𝑥𝑥5
+ 2𝑥𝑥2
− 4𝑥𝑥 + 1
12
3 2 -4coef
3 2 1exp
𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥
1 -10 -3 1coef
8 5 3 0exp
𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1
while ((aPos < terms) && (bPos < b.terms)) {
if (termArray[aPos].exp == b.termArray[bPos].exp) {
float t = termArray[aPos].coef + b.termArray[bPos].coef;
if (t) c.NewTerm(t, termArray[aPos].exp);
aPos++; bPos++;
}
else if (termArray[aPos].exp < b.termArray[bPos].exp) {
c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);
bPos++;
}
else {
c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);
aPos++;
}
}
aPos
bPos
𝑐𝑐 𝑥𝑥 = 𝑥𝑥8
− 10𝑥𝑥5
+ 2𝑥𝑥2
− 4𝑥𝑥 + 1
a.termArray[aPos].exp = a.termArray[0].exp = 3
b.termArray[bPos].exp = b.termArray[0].exp = 8
∴ a.termArray[aPos].exp < b.termArray[bPos].exp
coef
8
1
exp
13
3 2 -4coef
3 2 1exp
𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥
1 -10 -3 1coef
8 5 3 0exp
𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1
while ((aPos < terms) && (bPos < b.terms)) {
if (termArray[aPos].exp == b.termArray[bPos].exp) {
float t = termArray[aPos].coef + b.termArray[bPos].coef;
if (t) c.NewTerm(t, termArray[aPos].exp);
aPos++; bPos++;
}
else if (termArray[aPos].exp < b.termArray[bPos].exp) {
c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);
bPos++;
}
else {
c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);
aPos++;
}
}
aPos
bPos
𝑐𝑐 𝑥𝑥 = 𝑥𝑥8
− 10𝑥𝑥5
+ 2𝑥𝑥2
− 4𝑥𝑥 + 1
a.termArray[aPos].exp = a.termArray[0].exp = 3
b.termArray[bPos].exp = b.termArray[1].exp = 5
∴ a.termArray[aPos].exp < b.termArray[bPos].exp
-10coef
8
1
5exp
14
3 2 -4coef
3 2 1exp
𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥
1 -10 -3 1coef
8 5 3 0exp
𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1
while ((aPos < terms) && (bPos < b.terms)) {
if (termArray[aPos].exp == b.termArray[bPos].exp) {
float t = termArray[aPos].coef + b.termArray[bPos].coef;
if (t) c.NewTerm(t, termArray[aPos].exp);
aPos++; bPos++;
}
else if (termArray[aPos].exp < b.termArray[bPos].exp) {
c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);
bPos++;
}
else {
c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);
aPos++;
}
}
aPos
bPos
𝑐𝑐 𝑥𝑥 = 𝑥𝑥8
− 10𝑥𝑥5
+ 2𝑥𝑥2
− 4𝑥𝑥 + 1
a.termArray[aPos].exp = a.termArray[0].exp = 3
b.termArray[bPos].exp = b.termArray[2].exp = 3
∴ a.termArray[aPos].exp == b.termArray[bPos].exp
-10coef
8
1
5exp
T = 3 – 3 = 0이므로 새 항은 만들어지지 않음
15
3 2 -4coef
3 2 1exp
𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥
1 -10 -3 1coef
8 5 3 0exp
𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1
while ((aPos < terms) && (bPos < b.terms)) {
if (termArray[aPos].exp == b.termArray[bPos].exp) {
float t = termArray[aPos].coef + b.termArray[bPos].coef;
if (t) c.NewTerm(t, termArray[aPos].exp);
aPos++; bPos++;
}
else if (termArray[aPos].exp < b.termArray[bPos].exp) {
c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);
bPos++;
}
else {
c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);
aPos++;
}
}
aPos
bPos
𝑐𝑐 𝑥𝑥 = 𝑥𝑥8
− 10𝑥𝑥5
+ 2𝑥𝑥2
− 4𝑥𝑥 + 1
a.termArray[aPos].exp = a.termArray[1].exp = 2
b.termArray[bPos].exp = b.termArray[3].exp = 0
∴ a.termArray[aPos].exp > b.termArray[bPos].exp
-10 2coef
8
1
5 2exp
16
3 2 -4coef
3 2 1exp
𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥
1 -10 -3 1coef
8 5 3 0exp
𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1
while ((aPos < terms) && (bPos < b.terms)) {
if (termArray[aPos].exp == b.termArray[bPos].exp) {
float t = termArray[aPos].coef + b.termArray[bPos].coef;
if (t) c.NewTerm(t, termArray[aPos].exp);
aPos++; bPos++;
}
else if (termArray[aPos].exp < b.termArray[bPos].exp) {
c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);
bPos++;
}
else {
c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);
aPos++;
}
}
aPos
bPos
𝑐𝑐 𝑥𝑥 = 𝑥𝑥8
− 10𝑥𝑥5
+ 2𝑥𝑥2
− 4𝑥𝑥 + 1
a.termArray[aPos].exp = a.termArray[2].exp = 1
b.termArray[bPos].exp = b.termArray[3].exp = 0
∴ a.termArray[aPos].exp > b.termArray[bPos].exp
-10 2 -4coef
8
1
5 2 1exp
17
𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥
1 -10 -3 1coef
8 5 3 0exp
𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1
// Add rest terms of *this
for (; aPos < terms; aPos++)
c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);
// Add rest terms of b(x)
for (; bPos < b.terms; bPos++)
c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);
bPos
𝑐𝑐 𝑥𝑥 = 𝑥𝑥8
− 10𝑥𝑥5
+ 2𝑥𝑥2
− 4𝑥𝑥 + 1
-10 12 -4coef
08
1
5 2 1exp
3 2 -4coef
3 2 1exp
aPos
18
20
21
// Three elements: <row, column, value>
// row, column: int (not negative)
// <row, column> is a unique pair
class SparseMatrix
{
public:
// Create SparseMatrix with r rows, c columns, t terms (not 0)
SparseMatrix(int r, int c, int t);
// Transpose all elements of *this and return it
SparseMatrix Transpose();
// If the dimension of *this equals to b, add/subtract *this and b and return result
// Otherwise, throw exception
SparseMatrix Add(const SparseMatrix& b);
SparseMatrix Subtract(const SparseMatrix& b);
// If the column of *this equals to the row of b,
// multiply *this and b and return result
// Otherwise, throw exception
SparseMatrix Multiply(const SparseMatrix& b);
};
22
class SparseMatrix; // Forward Declaration
class MatrixTerm
{
friend class SparseMatrix;
private:
int row, col, value;
};
class SparseMatrix
{
private:
int rows, cols, terms, capacity;
MatrixTerm* smArray;
...
};
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
23
2 × 2 2 × 2 2 × 2
=
A의 1행, B의 1열
A의 2행, B의 1열 A의 2행, B의 2열
A의 1행, B의 2열
24
25
SparseMatrix SparseMatrix::Transpose()
{ // Return the transpose matrix of *this
SparseMatrix b(cols, rows, terms); // the size of b.smArray is terms
if (terms > 0)
{ // This is not 0-matrix
int currentB = 0;
for (int c = 0; c < cols; c++)
{ // Transpose each column
for (int i = 0; i < terms; i++)
{ // Find element from column c and move it
if (smArray[i].col == c)
{
b.smArray[currentB].row = c;
b.smArray[currentB].col = smArray[i].row;
b.smArray[currentB++].value = smArray[i].value;
}
}
}
} // End of if (terms > 0)
return b;
}
26
27
SparseMatrix SparseMatrix::FastTranspose()
{ // Return the transpose matrix of *this in O(terms + cols)
SparseMatrix b(cols, rows, terms);
if (terms > 0)
{ // This is not 0-matrix
int* rowSize = new int[cols];
int* rowStart = new int[cols];
std::fill(rowSize, rowSize + cols, 0); // Initialization
for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++;
// Start point of row i of rowStart[i] = b
rowStart[0] = 0;
for (int i = 1; i < cols; i++)
rowStart[i] = rowStart[i - 1] + rowSize[i - 1];
28
for (int i = 0; i < terms; i++)
{ // Copy *this to b
int j = rowStart[smArray[i].col];
b.smArray[j].row = smArray[i].col;
b.smArray[j].col = smArray[i].row;
b.smArray[j].value = smArray[i].value;
rowStart[smArray[i].col]++;
} // End of for
delete[] rowSize;
delete[] rowStart;
} // End of if
return b;
}
29
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
int* rowSize = new int[cols];
int* rowStart = new int[cols];
std::fill(rowSize, rowSize + cols, 0); // Initialization
for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++;
[0] [1] [2] [3] [4] [5]
rowSize = 0 0 0 0 0 0
rowStart =
30
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
int* rowSize = new int[cols];
int* rowStart = new int[cols];
std::fill(rowSize, rowSize + cols, 0); // Initialization
for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++;
[0] [1] [2] [3] [4] [5]
rowSize = 1 0 0 0 0 0
rowStart =
i = 0 → smArray[0].col = 0 → rowSize[0]++;
31
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
int* rowSize = new int[cols];
int* rowStart = new int[cols];
std::fill(rowSize, rowSize + cols, 0); // Initialization
for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++;
[0] [1] [2] [3] [4] [5]
rowSize = 1 0 0 1 0 0
rowStart =
i = 1 → smArray[1].col = 3 → rowSize[3]++;
32
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
int* rowSize = new int[cols];
int* rowStart = new int[cols];
std::fill(rowSize, rowSize + cols, 0); // Initialization
for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++;
[0] [1] [2] [3] [4] [5]
rowSize = 1 0 0 1 0 1
rowStart =
i = 2 → smArray[2].col = 5 → rowSize[5]++;
33
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
int* rowSize = new int[cols];
int* rowStart = new int[cols];
std::fill(rowSize, rowSize + cols, 0); // Initialization
for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++;
[0] [1] [2] [3] [4] [5]
rowSize = 1 1 0 1 0 1
rowStart =
i = 3 → smArray[3].col = 1 → rowSize[1]++;
34
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
int* rowSize = new int[cols];
int* rowStart = new int[cols];
std::fill(rowSize, rowSize + cols, 0); // Initialization
for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++;
[0] [1] [2] [3] [4] [5]
rowSize = 1 1 1 1 0 1
rowStart =
i = 4 → smArray[4].col = 2 → rowSize[2]++;
35
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
int* rowSize = new int[cols];
int* rowStart = new int[cols];
std::fill(rowSize, rowSize + cols, 0); // Initialization
for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++;
[0] [1] [2] [3] [4] [5]
rowSize = 1 1 1 2 0 1
rowStart =
i = 5 → smArray[5].col = 3 → rowSize[3]++;
36
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
int* rowSize = new int[cols];
int* rowStart = new int[cols];
std::fill(rowSize, rowSize + cols, 0); // Initialization
for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++;
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 1 2 0 1
rowStart =
i = 6 → smArray[6].col = 0 → rowSize[0]++;
37
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
int* rowSize = new int[cols];
int* rowStart = new int[cols];
std::fill(rowSize, rowSize + cols, 0); // Initialization
for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++;
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart =
i = 7 → smArray[7].col = 2 → rowSize[2]++;
38
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
// Start point of row i of rowStart[i] = b
rowStart[0] = 0;
for (int i = 1; i < cols; i++)
rowStart[i] = rowStart[i - 1] + rowSize[i - 1];
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 0
39
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
// Start point of row i of rowStart[i] = b
rowStart[0] = 0;
for (int i = 1; i < cols; i++)
rowStart[i] = rowStart[i - 1] + rowSize[i - 1];
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 0 2
i = 1 → rowStart[1] = rowStart[0] + rowSize[0];
40
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
// Start point of row i of rowStart[i] = b
rowStart[0] = 0;
for (int i = 1; i < cols; i++)
rowStart[i] = rowStart[i - 1] + rowSize[i - 1];
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 0 2 3
i = 2 → rowStart[2] = rowStart[1] + rowSize[1];
41
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
// Start point of row i of rowStart[i] = b
rowStart[0] = 0;
for (int i = 1; i < cols; i++)
rowStart[i] = rowStart[i - 1] + rowSize[i - 1];
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 0 2 3 5
i = 3 → rowStart[3] = rowStart[2] + rowSize[2];
42
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
// Start point of row i of rowStart[i] = b
rowStart[0] = 0;
for (int i = 1; i < cols; i++)
rowStart[i] = rowStart[i - 1] + rowSize[i - 1];
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 0 2 3 5 7
i = 4 → rowStart[4] = rowStart[3] + rowSize[3];
43
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
// Start point of row i of rowStart[i] = b
rowStart[0] = 0;
for (int i = 1; i < cols; i++)
rowStart[i] = rowStart[i - 1] + rowSize[i - 1];
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 0 2 3 5 7 7
i = 5 → rowStart[5] = rowStart[4] + rowSize[4];
44
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 1 2 3 5 7 7
for (int i = 0; i < terms; i++)
{ // Copy *this to b
int j = rowStart[smArray[i].col];
b.smArray[j].row = smArray[i].col;
b.smArray[j].col = smArray[i].row;
b.smArray[j].value = smArray[i].value;
rowStart[smArray[i].col]++;
} // End of for
row col value
smArray[0] 0 0 15
smArray[1]
smArray[2]
smArray[3]
smArray[4]
smArray[5]
smArray[6]
smArray[7]
i = 0 → smArray[0].col = 0 → j = rowStart[0] = 0
rowStart[0]++;
45
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 1 2 3 6 7 7
for (int i = 0; i < terms; i++)
{ // Copy *this to b
int j = rowStart[smArray[i].col];
b.smArray[j].row = smArray[i].col;
b.smArray[j].col = smArray[i].row;
b.smArray[j].value = smArray[i].value;
rowStart[smArray[i].col]++;
} // End of for
row col value
smArray[0] 0 0 15
smArray[1]
smArray[2]
smArray[3]
smArray[4]
smArray[5] 3 0 22
smArray[6]
smArray[7]
i = 1 → smArray[1].col = 3 → j = rowStart[3] = 5
rowStart[3]++;
46
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 1 2 3 6 7 8
for (int i = 0; i < terms; i++)
{ // Copy *this to b
int j = rowStart[smArray[i].col];
b.smArray[j].row = smArray[i].col;
b.smArray[j].col = smArray[i].row;
b.smArray[j].value = smArray[i].value;
rowStart[smArray[i].col]++;
} // End of for
row col value
smArray[0] 0 0 15
smArray[1]
smArray[2]
smArray[3]
smArray[4]
smArray[5] 3 0 22
smArray[6]
smArray[7] 5 0 -15
i = 2 → smArray[2].col = 5 → j = rowStart[5] = 7
rowStart[5]++;
47
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 1 3 3 6 7 8
for (int i = 0; i < terms; i++)
{ // Copy *this to b
int j = rowStart[smArray[i].col];
b.smArray[j].row = smArray[i].col;
b.smArray[j].col = smArray[i].row;
b.smArray[j].value = smArray[i].value;
rowStart[smArray[i].col]++;
} // End of for
row col value
smArray[0] 0 0 15
smArray[1]
smArray[2] 1 1 11
smArray[3]
smArray[4]
smArray[5] 3 0 22
smArray[6]
smArray[7] 5 0 -15
i = 3 → smArray[3].col = 1 → j = rowStart[1] = 2
rowStart[1]++;
48
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 1 3 4 6 7 8
for (int i = 0; i < terms; i++)
{ // Copy *this to b
int j = rowStart[smArray[i].col];
b.smArray[j].row = smArray[i].col;
b.smArray[j].col = smArray[i].row;
b.smArray[j].value = smArray[i].value;
rowStart[smArray[i].col]++;
} // End of for
row col value
smArray[0] 0 0 15
smArray[1]
smArray[2] 1 1 11
smArray[3] 2 1 3
smArray[4]
smArray[5] 3 0 22
smArray[6]
smArray[7] 5 0 -15
i = 4 → smArray[4].col = 2 → j = rowStart[2] = 3
rowStart[2]++;
49
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 1 3 4 7 7 8
for (int i = 0; i < terms; i++)
{ // Copy *this to b
int j = rowStart[smArray[i].col];
b.smArray[j].row = smArray[i].col;
b.smArray[j].col = smArray[i].row;
b.smArray[j].value = smArray[i].value;
rowStart[smArray[i].col]++;
} // End of for
row col value
smArray[0] 0 0 15
smArray[1]
smArray[2] 1 1 11
smArray[3] 2 1 3
smArray[4]
smArray[5] 3 0 22
smArray[6] 3 2 -6
smArray[7] 5 0 -15
i = 5 → smArray[5].col = 3 → j = rowStart[3] = 6
rowStart[3]++;
50
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 2 3 4 7 7 8
for (int i = 0; i < terms; i++)
{ // Copy *this to b
int j = rowStart[smArray[i].col];
b.smArray[j].row = smArray[i].col;
b.smArray[j].col = smArray[i].row;
b.smArray[j].value = smArray[i].value;
rowStart[smArray[i].col]++;
} // End of for
row col value
smArray[0] 0 0 15
smArray[1] 0 4 91
smArray[2] 1 1 11
smArray[3] 2 1 3
smArray[4]
smArray[5] 3 0 22
smArray[6] 3 2 -6
smArray[7] 5 0 -15
i = 6 → smArray[6].col = 0 → j = rowStart[0] = 1
rowStart[0]++;
51
row col value
smArray[0] 0 0 15
smArray[1] 0 3 22
smArray[2] 0 5 -15
smArray[3] 1 1 11
smArray[4] 1 2 3
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28
[0] [1] [2] [3] [4] [5]
rowSize = 2 1 2 2 0 1
rowStart = 2 3 5 7 7 8
for (int i = 0; i < terms; i++)
{ // Copy *this to b
int j = rowStart[smArray[i].col];
b.smArray[j].row = smArray[i].col;
b.smArray[j].col = smArray[i].row;
b.smArray[j].value = smArray[i].value;
rowStart[smArray[i].col]++;
} // End of for
row col value
smArray[0] 0 0 15
smArray[1] 0 4 91
smArray[2] 1 1 11
smArray[3] 2 1 3
smArray[4] 2 5 28
smArray[5] 3 0 22
smArray[6] 3 2 -6
smArray[7] 5 0 -15
i = 7 → smArray[7].col = 2 → j = rowStart[2] = 4
rowStart[2]++;
52
54
class String {
public:
// Constructor with content *init, length m
String(char* init, int m);
// Return true if *this's string is equal to t's. Otherwise, return false
bool operator==(String t);
// Return true if *this's string is empty. Otherwise, return false
bool operator!();
// Return the number of *this's characters
int Length();
// Return *this's string + t's
String Concat(String t);
// Return substring with index (i, i+1, ..., i+j-1) of *this's string
String Substr(int i, int j);
// Return index i if *this's substring matches pat's string
// Return -1 if pat is empty or not *this's substring
int Find(String pat);
};
55
56
int String::Find(String pat)
{ // If pat is found in *this, then return start position of pat in *this
// Otherwise, return -1
for (int start = 0; start <= Length() - pat.Length(); start++)
{ // Check pattern match at str[start]
int j;
for (j = 0; j < pat.Length() && str[start + j] == pat.Substr[j]; j++)
if (j == pat.Length()) return start; // Found match
}
return -1; // pat is empty or not exist in *this
}
57
58
59
60
𝑝𝑝𝑝𝑝𝑝𝑝
𝑝𝑝𝑝𝑝𝑝𝑝 𝑏𝑏와 같으므로,
𝑠𝑠𝑖𝑖+1 ≠ 𝑎𝑎라는 것을 알기 때문에 𝑝𝑝𝑝𝑝𝑝𝑝의 첫 문자와 𝑠𝑠𝑖𝑖+1을 비교할 필요가 없음
𝑠𝑠 = - 𝑎𝑎 𝑏𝑏 ? ? ? . . . . ?
𝑝𝑝𝑝𝑝𝑝𝑝 = 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑐𝑐 𝑎𝑎 𝑏𝑏
61
𝑝𝑝𝑝𝑝𝑝𝑝
𝑝𝑝𝑝𝑝𝑝𝑝 𝑏𝑏를 비교
𝑝𝑝𝑝𝑝𝑝𝑝
𝑠𝑠 = - 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 ? ? . . . ?
𝑝𝑝𝑝𝑝𝑝𝑝 = 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑐𝑐 𝑎𝑎 𝑏𝑏
𝑠𝑠 = - 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 ? ? . . . ?
𝑝𝑝𝑝𝑝𝑝𝑝 = 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑐𝑐 𝑎𝑎 𝑏𝑏
62
𝑗𝑗 0 1 2 3 4 5 6 7 8 9
𝑝𝑝𝑝𝑝𝑝𝑝 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑐𝑐 𝑎𝑎 𝑏𝑏
𝑓𝑓 −1 −1 −1 0 1 2 3 −1 0 1
63
int String::FastFind(String pat)
{ // Determine whether pat is substring of s or not
int posP = 0, posS = 0;
int lengthP = pat.Length(), lengthS = Length();
while ((posP < lengthP) && (posS < lengthS)) {
if (pat.str[posP] == pat.str[posS]) { // String match
posP++; posS++;
}
else {
if (posP == 0) posS++;
else posP = pat.f[posP - 1] + 1;
}
}
if (posP < lengthP) return -1;
else return posS - lengthP;
}
64
65
66
void String::FailureFunction()
{ // Calculate failure function about *this pattern
int lengthP = Length();
f[0] = -1;
for (int j = 1; j < lengthP; j++)
{
int i = f[j - 1];
while ((*(str + j) != *(str + i + 1)) && (i >= 0)) i = f[i];
if (*(str + j) == *(str + i + 1)) f[j] = i + 1;
else f[j] = -1;
}
}
67
68

More Related Content

What's hot

What's hot (20)

Travel management
Travel managementTravel management
Travel management
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
 
week-16x
week-16xweek-16x
week-16x
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 
Implementing string
Implementing stringImplementing string
Implementing string
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
Oop1
Oop1Oop1
Oop1
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))
 
2. Базовый синтаксис Java
2. Базовый синтаксис Java2. Базовый синтаксис Java
2. Базовый синтаксис Java
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
9. pointer, pointer & function
9. pointer, pointer & function9. pointer, pointer & function
9. pointer, pointer & function
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)
 
C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
 
Nesting of for loops using C++
Nesting of for loops using C++Nesting of for loops using C++
Nesting of for loops using C++
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
 

Viewers also liked

[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기Chris Ohk
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features SummaryChris Ohk
 
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기Chris Ohk
 
C++ Programming - 10th Study
C++ Programming - 10th StudyC++ Programming - 10th Study
C++ Programming - 10th StudyChris Ohk
 
C++ Programming - 13th Study
C++ Programming - 13th StudyC++ Programming - 13th Study
C++ Programming - 13th StudyChris Ohk
 
C++ Programming - 8th Study
C++ Programming - 8th StudyC++ Programming - 8th Study
C++ Programming - 8th StudyChris Ohk
 
C++ Programming - 9th Study
C++ Programming - 9th StudyC++ Programming - 9th Study
C++ Programming - 9th StudyChris Ohk
 
C++ Programming - 7th Study
C++ Programming - 7th StudyC++ Programming - 7th Study
C++ Programming - 7th StudyChris Ohk
 
C++ Programming - 12th Study
C++ Programming - 12th StudyC++ Programming - 12th Study
C++ Programming - 12th StudyChris Ohk
 
NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험
NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험
NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험Jooeun Park
 
Data Structure - 1st Study
Data Structure - 1st StudyData Structure - 1st Study
Data Structure - 1st StudyChris Ohk
 
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)Sang Don Kim
 

Viewers also liked (12)

[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary
 
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
 
C++ Programming - 10th Study
C++ Programming - 10th StudyC++ Programming - 10th Study
C++ Programming - 10th Study
 
C++ Programming - 13th Study
C++ Programming - 13th StudyC++ Programming - 13th Study
C++ Programming - 13th Study
 
C++ Programming - 8th Study
C++ Programming - 8th StudyC++ Programming - 8th Study
C++ Programming - 8th Study
 
C++ Programming - 9th Study
C++ Programming - 9th StudyC++ Programming - 9th Study
C++ Programming - 9th Study
 
C++ Programming - 7th Study
C++ Programming - 7th StudyC++ Programming - 7th Study
C++ Programming - 7th Study
 
C++ Programming - 12th Study
C++ Programming - 12th StudyC++ Programming - 12th Study
C++ Programming - 12th Study
 
NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험
NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험
NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험
 
Data Structure - 1st Study
Data Structure - 1st StudyData Structure - 1st Study
Data Structure - 1st Study
 
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)
 

Similar to Data Structure - 2nd Study

Raices de ecuaciones
Raices de ecuacionesRaices de ecuaciones
Raices de ecuacionesNatalia
 
Raices de ecuaciones
Raices de ecuacionesRaices de ecuaciones
Raices de ecuacionesNatalia
 
First C++ code written... I think its quite efficient with all the.pdf
First C++ code written... I think its quite efficient with all the.pdfFirst C++ code written... I think its quite efficient with all the.pdf
First C++ code written... I think its quite efficient with all the.pdfankit482504
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0Yaser Zhian
 
I wrote the following change it to having a header, main and cpp fi.pdf
I wrote the following change it to having a header, main and cpp fi.pdfI wrote the following change it to having a header, main and cpp fi.pdf
I wrote the following change it to having a header, main and cpp fi.pdfrishteygallery
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework HelpC++ Homework Help
 
C++ extension methods
C++ extension methodsC++ extension methods
C++ extension methodsphil_nash
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 
ITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
ITGM #9 - Коварный CodeType, или от segfault'а к работающему кодуITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
ITGM #9 - Коварный CodeType, или от segfault'а к работающему кодуdelimitry
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfforladies
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)ujihisa
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...GkhanGirgin3
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...ssuserd6b1fd
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisaujihisa
 

Similar to Data Structure - 2nd Study (20)

Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Raices de ecuaciones
Raices de ecuacionesRaices de ecuaciones
Raices de ecuaciones
 
Raices de ecuaciones
Raices de ecuacionesRaices de ecuaciones
Raices de ecuaciones
 
First C++ code written... I think its quite efficient with all the.pdf
First C++ code written... I think its quite efficient with all the.pdfFirst C++ code written... I think its quite efficient with all the.pdf
First C++ code written... I think its quite efficient with all the.pdf
 
Array
ArrayArray
Array
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0
 
I wrote the following change it to having a header, main and cpp fi.pdf
I wrote the following change it to having a header, main and cpp fi.pdfI wrote the following change it to having a header, main and cpp fi.pdf
I wrote the following change it to having a header, main and cpp fi.pdf
 
Arrays
ArraysArrays
Arrays
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
C++ extension methods
C++ extension methodsC++ extension methods
C++ extension methods
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Programming Homework Help
Programming Homework Help Programming Homework Help
Programming Homework Help
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
ITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
ITGM #9 - Коварный CodeType, или от segfault'а к работающему кодуITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
ITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
 

More from Chris Ohk

인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍Chris Ohk
 
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들Chris Ohk
 
Momenti Seminar - 5 Years of RosettaStone
Momenti Seminar - 5 Years of RosettaStoneMomenti Seminar - 5 Years of RosettaStone
Momenti Seminar - 5 Years of RosettaStoneChris Ohk
 
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기Chris Ohk
 
Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 2Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 2Chris Ohk
 
Momenti Seminar - A Tour of Rust, Part 1
Momenti Seminar - A Tour of Rust, Part 1Momenti Seminar - A Tour of Rust, Part 1
Momenti Seminar - A Tour of Rust, Part 1Chris Ohk
 
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021Chris Ohk
 
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021Chris Ohk
 
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020Chris Ohk
 
Proximal Policy Optimization Algorithms, Schulman et al, 2017
Proximal Policy Optimization Algorithms, Schulman et al, 2017Proximal Policy Optimization Algorithms, Schulman et al, 2017
Proximal Policy Optimization Algorithms, Schulman et al, 2017Chris Ohk
 
Trust Region Policy Optimization, Schulman et al, 2015
Trust Region Policy Optimization, Schulman et al, 2015Trust Region Policy Optimization, Schulman et al, 2015
Trust Region Policy Optimization, Schulman et al, 2015Chris Ohk
 
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015Chris Ohk
 
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기Chris Ohk
 
[RLKorea] <하스스톤> 강화학습 환경 개발기
[RLKorea] <하스스톤> 강화학습 환경 개발기[RLKorea] <하스스톤> 강화학습 환경 개발기
[RLKorea] <하스스톤> 강화학습 환경 개발기Chris Ohk
 
[NDC 2019] 하스스톤 강화학습 환경 개발기
[NDC 2019] 하스스톤 강화학습 환경 개발기[NDC 2019] 하스스톤 강화학습 환경 개발기
[NDC 2019] 하스스톤 강화학습 환경 개발기Chris Ohk
 
C++20 Key Features Summary
C++20 Key Features SummaryC++20 Key Features Summary
C++20 Key Features SummaryChris Ohk
 
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지Chris Ohk
 
디미고 특강 - 개발을 시작하려는 여러분에게
디미고 특강 - 개발을 시작하려는 여러분에게디미고 특강 - 개발을 시작하려는 여러분에게
디미고 특강 - 개발을 시작하려는 여러분에게Chris Ohk
 
청강대 특강 - 프로젝트 제대로 해보기
청강대 특강 - 프로젝트 제대로 해보기청강대 특강 - 프로젝트 제대로 해보기
청강대 특강 - 프로젝트 제대로 해보기Chris Ohk
 
[NDC 2018] 유체역학 엔진 개발기
[NDC 2018] 유체역학 엔진 개발기[NDC 2018] 유체역학 엔진 개발기
[NDC 2018] 유체역학 엔진 개발기Chris Ohk
 

More from Chris Ohk (20)

인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
 
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
 
Momenti Seminar - 5 Years of RosettaStone
Momenti Seminar - 5 Years of RosettaStoneMomenti Seminar - 5 Years of RosettaStone
Momenti Seminar - 5 Years of RosettaStone
 
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기
 
Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 2Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 2
 
Momenti Seminar - A Tour of Rust, Part 1
Momenti Seminar - A Tour of Rust, Part 1Momenti Seminar - A Tour of Rust, Part 1
Momenti Seminar - A Tour of Rust, Part 1
 
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021
 
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021
 
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020
 
Proximal Policy Optimization Algorithms, Schulman et al, 2017
Proximal Policy Optimization Algorithms, Schulman et al, 2017Proximal Policy Optimization Algorithms, Schulman et al, 2017
Proximal Policy Optimization Algorithms, Schulman et al, 2017
 
Trust Region Policy Optimization, Schulman et al, 2015
Trust Region Policy Optimization, Schulman et al, 2015Trust Region Policy Optimization, Schulman et al, 2015
Trust Region Policy Optimization, Schulman et al, 2015
 
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015
 
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기
 
[RLKorea] <하스스톤> 강화학습 환경 개발기
[RLKorea] <하스스톤> 강화학습 환경 개발기[RLKorea] <하스스톤> 강화학습 환경 개발기
[RLKorea] <하스스톤> 강화학습 환경 개발기
 
[NDC 2019] 하스스톤 강화학습 환경 개발기
[NDC 2019] 하스스톤 강화학습 환경 개발기[NDC 2019] 하스스톤 강화학습 환경 개발기
[NDC 2019] 하스스톤 강화학습 환경 개발기
 
C++20 Key Features Summary
C++20 Key Features SummaryC++20 Key Features Summary
C++20 Key Features Summary
 
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지
 
디미고 특강 - 개발을 시작하려는 여러분에게
디미고 특강 - 개발을 시작하려는 여러분에게디미고 특강 - 개발을 시작하려는 여러분에게
디미고 특강 - 개발을 시작하려는 여러분에게
 
청강대 특강 - 프로젝트 제대로 해보기
청강대 특강 - 프로젝트 제대로 해보기청강대 특강 - 프로젝트 제대로 해보기
청강대 특강 - 프로젝트 제대로 해보기
 
[NDC 2018] 유체역학 엔진 개발기
[NDC 2018] 유체역학 엔진 개발기[NDC 2018] 유체역학 엔진 개발기
[NDC 2018] 유체역학 엔진 개발기
 

Recently uploaded

Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 

Recently uploaded (20)

Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 

Data Structure - 2nd Study

  • 1.
  • 2.
  • 3. 3
  • 4. 4 // Polynomial: A set of pairs, <e_i, a_i> // a_i: coefficient (not 0, float type) // e_i: exponent (not negative, int type) class Polynomial { public: // Create polynomial p(x) = 0 Polynomial(); // Add polynomial *this and poly const Polynomial Add(const Polynomial& poly); // Multiply polynomial *this and poly const Polynomial Multiply(const Polynomial& poly); // After assign f into x, evaluate polynomial and return result const float Eval(const float& f); };
  • 5. 5 private: static const int MaxDegree = 100; int degree; // degree <= MaxDegree float coef[MaxDegree + 1]; // Coefficient array
  • 6. 6 private: int degree; float* coef; // Coefficient array Polynomial::Polynomial(int d) : degree(d), coef(new float[degree + 1]) { }
  • 7. 7 class Polynomial; // Forward Declaration class Term { friend Polynomial; private: float coef; // Coefficient int exp; // Exponent }; class Polynomial { private: Term* termArray; // Term array (not 0) int capacity; // Capacity of termArray int terms; // Number of terms (not 0) ... }; 3 2 -4coef 3 2 1exp 𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥 1 -10 -3 1coef 8 5 3 0exp 𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1
  • 8. 8 const Polynomial Polynomial::Add(const Polynomial& b) { // Return *this + b Polynomial c; int aPos = 0, bPos = 0; while ((aPos < terms) && (bPos < b.terms)) { if (termArray[aPos].exp == b.termArray[bPos].exp) { float t = termArray[aPos].coef + b.termArray[bPos].coef; if (t) c.NewTerm(t, termArray[aPos].exp); aPos++; bPos++; } else if (termArray[aPos].exp < b.termArray[bPos].exp) { c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp); bPos++; } else { c.NewTerm(termArray[aPos].coef, termArray[aPos].exp); aPos++; } }
  • 9. 9 // Add rest terms of *this for (; aPos < terms; aPos++) c.NewTerm(termArray[aPos].coef, termArray[aPos].exp); // Add rest terms of b(x) for (; bPos < b.terms; bPos++) c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp); return c; }
  • 10. 10 void Polynomial::NewTerm(const float& coef, const int& exp) { // Add new term into into last element of termArray if (terms == capacity) { // Expand the size of termArray twice capacity *= 2; Term* temp = new Term[capacity];// New array std::copy(termArray, termArray + terms, temp); delete[] termArray;// Return original array termArray = temp; } termArray[terms].coef = coef; termArray[terms++].exp = exp; }
  • 11. 11 Polynomial c; int aPos = 0, bPos = 0; 3 2 -4coef 3 2 1exp 𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥 1 -10 -3 1coef 8 5 3 0exp 𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1 aPos bPos 𝑐𝑐 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 + 2𝑥𝑥2 − 4𝑥𝑥 + 1
  • 12. 12 3 2 -4coef 3 2 1exp 𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥 1 -10 -3 1coef 8 5 3 0exp 𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1 while ((aPos < terms) && (bPos < b.terms)) { if (termArray[aPos].exp == b.termArray[bPos].exp) { float t = termArray[aPos].coef + b.termArray[bPos].coef; if (t) c.NewTerm(t, termArray[aPos].exp); aPos++; bPos++; } else if (termArray[aPos].exp < b.termArray[bPos].exp) { c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp); bPos++; } else { c.NewTerm(termArray[aPos].coef, termArray[aPos].exp); aPos++; } } aPos bPos 𝑐𝑐 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 + 2𝑥𝑥2 − 4𝑥𝑥 + 1 a.termArray[aPos].exp = a.termArray[0].exp = 3 b.termArray[bPos].exp = b.termArray[0].exp = 8 ∴ a.termArray[aPos].exp < b.termArray[bPos].exp coef 8 1 exp
  • 13. 13 3 2 -4coef 3 2 1exp 𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥 1 -10 -3 1coef 8 5 3 0exp 𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1 while ((aPos < terms) && (bPos < b.terms)) { if (termArray[aPos].exp == b.termArray[bPos].exp) { float t = termArray[aPos].coef + b.termArray[bPos].coef; if (t) c.NewTerm(t, termArray[aPos].exp); aPos++; bPos++; } else if (termArray[aPos].exp < b.termArray[bPos].exp) { c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp); bPos++; } else { c.NewTerm(termArray[aPos].coef, termArray[aPos].exp); aPos++; } } aPos bPos 𝑐𝑐 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 + 2𝑥𝑥2 − 4𝑥𝑥 + 1 a.termArray[aPos].exp = a.termArray[0].exp = 3 b.termArray[bPos].exp = b.termArray[1].exp = 5 ∴ a.termArray[aPos].exp < b.termArray[bPos].exp -10coef 8 1 5exp
  • 14. 14 3 2 -4coef 3 2 1exp 𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥 1 -10 -3 1coef 8 5 3 0exp 𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1 while ((aPos < terms) && (bPos < b.terms)) { if (termArray[aPos].exp == b.termArray[bPos].exp) { float t = termArray[aPos].coef + b.termArray[bPos].coef; if (t) c.NewTerm(t, termArray[aPos].exp); aPos++; bPos++; } else if (termArray[aPos].exp < b.termArray[bPos].exp) { c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp); bPos++; } else { c.NewTerm(termArray[aPos].coef, termArray[aPos].exp); aPos++; } } aPos bPos 𝑐𝑐 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 + 2𝑥𝑥2 − 4𝑥𝑥 + 1 a.termArray[aPos].exp = a.termArray[0].exp = 3 b.termArray[bPos].exp = b.termArray[2].exp = 3 ∴ a.termArray[aPos].exp == b.termArray[bPos].exp -10coef 8 1 5exp T = 3 – 3 = 0이므로 새 항은 만들어지지 않음
  • 15. 15 3 2 -4coef 3 2 1exp 𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥 1 -10 -3 1coef 8 5 3 0exp 𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1 while ((aPos < terms) && (bPos < b.terms)) { if (termArray[aPos].exp == b.termArray[bPos].exp) { float t = termArray[aPos].coef + b.termArray[bPos].coef; if (t) c.NewTerm(t, termArray[aPos].exp); aPos++; bPos++; } else if (termArray[aPos].exp < b.termArray[bPos].exp) { c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp); bPos++; } else { c.NewTerm(termArray[aPos].coef, termArray[aPos].exp); aPos++; } } aPos bPos 𝑐𝑐 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 + 2𝑥𝑥2 − 4𝑥𝑥 + 1 a.termArray[aPos].exp = a.termArray[1].exp = 2 b.termArray[bPos].exp = b.termArray[3].exp = 0 ∴ a.termArray[aPos].exp > b.termArray[bPos].exp -10 2coef 8 1 5 2exp
  • 16. 16 3 2 -4coef 3 2 1exp 𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥 1 -10 -3 1coef 8 5 3 0exp 𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1 while ((aPos < terms) && (bPos < b.terms)) { if (termArray[aPos].exp == b.termArray[bPos].exp) { float t = termArray[aPos].coef + b.termArray[bPos].coef; if (t) c.NewTerm(t, termArray[aPos].exp); aPos++; bPos++; } else if (termArray[aPos].exp < b.termArray[bPos].exp) { c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp); bPos++; } else { c.NewTerm(termArray[aPos].coef, termArray[aPos].exp); aPos++; } } aPos bPos 𝑐𝑐 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 + 2𝑥𝑥2 − 4𝑥𝑥 + 1 a.termArray[aPos].exp = a.termArray[2].exp = 1 b.termArray[bPos].exp = b.termArray[3].exp = 0 ∴ a.termArray[aPos].exp > b.termArray[bPos].exp -10 2 -4coef 8 1 5 2 1exp
  • 17. 17 𝑎𝑎 𝑥𝑥 = 3𝑥𝑥3 + 2𝑥𝑥2 − 4𝑥𝑥 1 -10 -3 1coef 8 5 3 0exp 𝑏𝑏 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 − 3𝑥𝑥3 + 1 // Add rest terms of *this for (; aPos < terms; aPos++) c.NewTerm(termArray[aPos].coef, termArray[aPos].exp); // Add rest terms of b(x) for (; bPos < b.terms; bPos++) c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp); bPos 𝑐𝑐 𝑥𝑥 = 𝑥𝑥8 − 10𝑥𝑥5 + 2𝑥𝑥2 − 4𝑥𝑥 + 1 -10 12 -4coef 08 1 5 2 1exp 3 2 -4coef 3 2 1exp aPos
  • 18. 18
  • 19.
  • 20. 20
  • 21. 21 // Three elements: <row, column, value> // row, column: int (not negative) // <row, column> is a unique pair class SparseMatrix { public: // Create SparseMatrix with r rows, c columns, t terms (not 0) SparseMatrix(int r, int c, int t); // Transpose all elements of *this and return it SparseMatrix Transpose(); // If the dimension of *this equals to b, add/subtract *this and b and return result // Otherwise, throw exception SparseMatrix Add(const SparseMatrix& b); SparseMatrix Subtract(const SparseMatrix& b); // If the column of *this equals to the row of b, // multiply *this and b and return result // Otherwise, throw exception SparseMatrix Multiply(const SparseMatrix& b); };
  • 22. 22 class SparseMatrix; // Forward Declaration class MatrixTerm { friend class SparseMatrix; private: int row, col, value; }; class SparseMatrix { private: int rows, cols, terms, capacity; MatrixTerm* smArray; ... }; row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28
  • 23. 23 2 × 2 2 × 2 2 × 2 = A의 1행, B의 1열 A의 2행, B의 1열 A의 2행, B의 2열 A의 1행, B의 2열
  • 24. 24
  • 25. 25 SparseMatrix SparseMatrix::Transpose() { // Return the transpose matrix of *this SparseMatrix b(cols, rows, terms); // the size of b.smArray is terms if (terms > 0) { // This is not 0-matrix int currentB = 0; for (int c = 0; c < cols; c++) { // Transpose each column for (int i = 0; i < terms; i++) { // Find element from column c and move it if (smArray[i].col == c) { b.smArray[currentB].row = c; b.smArray[currentB].col = smArray[i].row; b.smArray[currentB++].value = smArray[i].value; } } } } // End of if (terms > 0) return b; }
  • 26. 26
  • 27. 27 SparseMatrix SparseMatrix::FastTranspose() { // Return the transpose matrix of *this in O(terms + cols) SparseMatrix b(cols, rows, terms); if (terms > 0) { // This is not 0-matrix int* rowSize = new int[cols]; int* rowStart = new int[cols]; std::fill(rowSize, rowSize + cols, 0); // Initialization for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++; // Start point of row i of rowStart[i] = b rowStart[0] = 0; for (int i = 1; i < cols; i++) rowStart[i] = rowStart[i - 1] + rowSize[i - 1];
  • 28. 28 for (int i = 0; i < terms; i++) { // Copy *this to b int j = rowStart[smArray[i].col]; b.smArray[j].row = smArray[i].col; b.smArray[j].col = smArray[i].row; b.smArray[j].value = smArray[i].value; rowStart[smArray[i].col]++; } // End of for delete[] rowSize; delete[] rowStart; } // End of if return b; }
  • 29. 29 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 int* rowSize = new int[cols]; int* rowStart = new int[cols]; std::fill(rowSize, rowSize + cols, 0); // Initialization for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++; [0] [1] [2] [3] [4] [5] rowSize = 0 0 0 0 0 0 rowStart =
  • 30. 30 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 int* rowSize = new int[cols]; int* rowStart = new int[cols]; std::fill(rowSize, rowSize + cols, 0); // Initialization for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++; [0] [1] [2] [3] [4] [5] rowSize = 1 0 0 0 0 0 rowStart = i = 0 → smArray[0].col = 0 → rowSize[0]++;
  • 31. 31 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 int* rowSize = new int[cols]; int* rowStart = new int[cols]; std::fill(rowSize, rowSize + cols, 0); // Initialization for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++; [0] [1] [2] [3] [4] [5] rowSize = 1 0 0 1 0 0 rowStart = i = 1 → smArray[1].col = 3 → rowSize[3]++;
  • 32. 32 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 int* rowSize = new int[cols]; int* rowStart = new int[cols]; std::fill(rowSize, rowSize + cols, 0); // Initialization for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++; [0] [1] [2] [3] [4] [5] rowSize = 1 0 0 1 0 1 rowStart = i = 2 → smArray[2].col = 5 → rowSize[5]++;
  • 33. 33 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 int* rowSize = new int[cols]; int* rowStart = new int[cols]; std::fill(rowSize, rowSize + cols, 0); // Initialization for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++; [0] [1] [2] [3] [4] [5] rowSize = 1 1 0 1 0 1 rowStart = i = 3 → smArray[3].col = 1 → rowSize[1]++;
  • 34. 34 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 int* rowSize = new int[cols]; int* rowStart = new int[cols]; std::fill(rowSize, rowSize + cols, 0); // Initialization for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++; [0] [1] [2] [3] [4] [5] rowSize = 1 1 1 1 0 1 rowStart = i = 4 → smArray[4].col = 2 → rowSize[2]++;
  • 35. 35 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 int* rowSize = new int[cols]; int* rowStart = new int[cols]; std::fill(rowSize, rowSize + cols, 0); // Initialization for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++; [0] [1] [2] [3] [4] [5] rowSize = 1 1 1 2 0 1 rowStart = i = 5 → smArray[5].col = 3 → rowSize[3]++;
  • 36. 36 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 int* rowSize = new int[cols]; int* rowStart = new int[cols]; std::fill(rowSize, rowSize + cols, 0); // Initialization for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++; [0] [1] [2] [3] [4] [5] rowSize = 2 1 1 2 0 1 rowStart = i = 6 → smArray[6].col = 0 → rowSize[0]++;
  • 37. 37 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 int* rowSize = new int[cols]; int* rowStart = new int[cols]; std::fill(rowSize, rowSize + cols, 0); // Initialization for (int i = 0; i < terms; i++) rowSize[smArray[i].col]++; [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = i = 7 → smArray[7].col = 2 → rowSize[2]++;
  • 38. 38 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 // Start point of row i of rowStart[i] = b rowStart[0] = 0; for (int i = 1; i < cols; i++) rowStart[i] = rowStart[i - 1] + rowSize[i - 1]; [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 0
  • 39. 39 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 // Start point of row i of rowStart[i] = b rowStart[0] = 0; for (int i = 1; i < cols; i++) rowStart[i] = rowStart[i - 1] + rowSize[i - 1]; [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 0 2 i = 1 → rowStart[1] = rowStart[0] + rowSize[0];
  • 40. 40 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 // Start point of row i of rowStart[i] = b rowStart[0] = 0; for (int i = 1; i < cols; i++) rowStart[i] = rowStart[i - 1] + rowSize[i - 1]; [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 0 2 3 i = 2 → rowStart[2] = rowStart[1] + rowSize[1];
  • 41. 41 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 // Start point of row i of rowStart[i] = b rowStart[0] = 0; for (int i = 1; i < cols; i++) rowStart[i] = rowStart[i - 1] + rowSize[i - 1]; [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 0 2 3 5 i = 3 → rowStart[3] = rowStart[2] + rowSize[2];
  • 42. 42 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 // Start point of row i of rowStart[i] = b rowStart[0] = 0; for (int i = 1; i < cols; i++) rowStart[i] = rowStart[i - 1] + rowSize[i - 1]; [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 0 2 3 5 7 i = 4 → rowStart[4] = rowStart[3] + rowSize[3];
  • 43. 43 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 // Start point of row i of rowStart[i] = b rowStart[0] = 0; for (int i = 1; i < cols; i++) rowStart[i] = rowStart[i - 1] + rowSize[i - 1]; [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 0 2 3 5 7 7 i = 5 → rowStart[5] = rowStart[4] + rowSize[4];
  • 44. 44 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 1 2 3 5 7 7 for (int i = 0; i < terms; i++) { // Copy *this to b int j = rowStart[smArray[i].col]; b.smArray[j].row = smArray[i].col; b.smArray[j].col = smArray[i].row; b.smArray[j].value = smArray[i].value; rowStart[smArray[i].col]++; } // End of for row col value smArray[0] 0 0 15 smArray[1] smArray[2] smArray[3] smArray[4] smArray[5] smArray[6] smArray[7] i = 0 → smArray[0].col = 0 → j = rowStart[0] = 0 rowStart[0]++;
  • 45. 45 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 1 2 3 6 7 7 for (int i = 0; i < terms; i++) { // Copy *this to b int j = rowStart[smArray[i].col]; b.smArray[j].row = smArray[i].col; b.smArray[j].col = smArray[i].row; b.smArray[j].value = smArray[i].value; rowStart[smArray[i].col]++; } // End of for row col value smArray[0] 0 0 15 smArray[1] smArray[2] smArray[3] smArray[4] smArray[5] 3 0 22 smArray[6] smArray[7] i = 1 → smArray[1].col = 3 → j = rowStart[3] = 5 rowStart[3]++;
  • 46. 46 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 1 2 3 6 7 8 for (int i = 0; i < terms; i++) { // Copy *this to b int j = rowStart[smArray[i].col]; b.smArray[j].row = smArray[i].col; b.smArray[j].col = smArray[i].row; b.smArray[j].value = smArray[i].value; rowStart[smArray[i].col]++; } // End of for row col value smArray[0] 0 0 15 smArray[1] smArray[2] smArray[3] smArray[4] smArray[5] 3 0 22 smArray[6] smArray[7] 5 0 -15 i = 2 → smArray[2].col = 5 → j = rowStart[5] = 7 rowStart[5]++;
  • 47. 47 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 1 3 3 6 7 8 for (int i = 0; i < terms; i++) { // Copy *this to b int j = rowStart[smArray[i].col]; b.smArray[j].row = smArray[i].col; b.smArray[j].col = smArray[i].row; b.smArray[j].value = smArray[i].value; rowStart[smArray[i].col]++; } // End of for row col value smArray[0] 0 0 15 smArray[1] smArray[2] 1 1 11 smArray[3] smArray[4] smArray[5] 3 0 22 smArray[6] smArray[7] 5 0 -15 i = 3 → smArray[3].col = 1 → j = rowStart[1] = 2 rowStart[1]++;
  • 48. 48 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 1 3 4 6 7 8 for (int i = 0; i < terms; i++) { // Copy *this to b int j = rowStart[smArray[i].col]; b.smArray[j].row = smArray[i].col; b.smArray[j].col = smArray[i].row; b.smArray[j].value = smArray[i].value; rowStart[smArray[i].col]++; } // End of for row col value smArray[0] 0 0 15 smArray[1] smArray[2] 1 1 11 smArray[3] 2 1 3 smArray[4] smArray[5] 3 0 22 smArray[6] smArray[7] 5 0 -15 i = 4 → smArray[4].col = 2 → j = rowStart[2] = 3 rowStart[2]++;
  • 49. 49 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 1 3 4 7 7 8 for (int i = 0; i < terms; i++) { // Copy *this to b int j = rowStart[smArray[i].col]; b.smArray[j].row = smArray[i].col; b.smArray[j].col = smArray[i].row; b.smArray[j].value = smArray[i].value; rowStart[smArray[i].col]++; } // End of for row col value smArray[0] 0 0 15 smArray[1] smArray[2] 1 1 11 smArray[3] 2 1 3 smArray[4] smArray[5] 3 0 22 smArray[6] 3 2 -6 smArray[7] 5 0 -15 i = 5 → smArray[5].col = 3 → j = rowStart[3] = 6 rowStart[3]++;
  • 50. 50 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 2 3 4 7 7 8 for (int i = 0; i < terms; i++) { // Copy *this to b int j = rowStart[smArray[i].col]; b.smArray[j].row = smArray[i].col; b.smArray[j].col = smArray[i].row; b.smArray[j].value = smArray[i].value; rowStart[smArray[i].col]++; } // End of for row col value smArray[0] 0 0 15 smArray[1] 0 4 91 smArray[2] 1 1 11 smArray[3] 2 1 3 smArray[4] smArray[5] 3 0 22 smArray[6] 3 2 -6 smArray[7] 5 0 -15 i = 6 → smArray[6].col = 0 → j = rowStart[0] = 1 rowStart[0]++;
  • 51. 51 row col value smArray[0] 0 0 15 smArray[1] 0 3 22 smArray[2] 0 5 -15 smArray[3] 1 1 11 smArray[4] 1 2 3 smArray[5] 2 3 -6 smArray[6] 4 0 91 smArray[7] 5 2 28 [0] [1] [2] [3] [4] [5] rowSize = 2 1 2 2 0 1 rowStart = 2 3 5 7 7 8 for (int i = 0; i < terms; i++) { // Copy *this to b int j = rowStart[smArray[i].col]; b.smArray[j].row = smArray[i].col; b.smArray[j].col = smArray[i].row; b.smArray[j].value = smArray[i].value; rowStart[smArray[i].col]++; } // End of for row col value smArray[0] 0 0 15 smArray[1] 0 4 91 smArray[2] 1 1 11 smArray[3] 2 1 3 smArray[4] 2 5 28 smArray[5] 3 0 22 smArray[6] 3 2 -6 smArray[7] 5 0 -15 i = 7 → smArray[7].col = 2 → j = rowStart[2] = 4 rowStart[2]++;
  • 52. 52
  • 53.
  • 54. 54 class String { public: // Constructor with content *init, length m String(char* init, int m); // Return true if *this's string is equal to t's. Otherwise, return false bool operator==(String t); // Return true if *this's string is empty. Otherwise, return false bool operator!(); // Return the number of *this's characters int Length(); // Return *this's string + t's String Concat(String t); // Return substring with index (i, i+1, ..., i+j-1) of *this's string String Substr(int i, int j); // Return index i if *this's substring matches pat's string // Return -1 if pat is empty or not *this's substring int Find(String pat); };
  • 55. 55
  • 56. 56 int String::Find(String pat) { // If pat is found in *this, then return start position of pat in *this // Otherwise, return -1 for (int start = 0; start <= Length() - pat.Length(); start++) { // Check pattern match at str[start] int j; for (j = 0; j < pat.Length() && str[start + j] == pat.Substr[j]; j++) if (j == pat.Length()) return start; // Found match } return -1; // pat is empty or not exist in *this }
  • 57. 57
  • 58. 58
  • 59. 59
  • 60. 60 𝑝𝑝𝑝𝑝𝑝𝑝 𝑝𝑝𝑝𝑝𝑝𝑝 𝑏𝑏와 같으므로, 𝑠𝑠𝑖𝑖+1 ≠ 𝑎𝑎라는 것을 알기 때문에 𝑝𝑝𝑝𝑝𝑝𝑝의 첫 문자와 𝑠𝑠𝑖𝑖+1을 비교할 필요가 없음 𝑠𝑠 = - 𝑎𝑎 𝑏𝑏 ? ? ? . . . . ? 𝑝𝑝𝑝𝑝𝑝𝑝 = 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑐𝑐 𝑎𝑎 𝑏𝑏
  • 61. 61 𝑝𝑝𝑝𝑝𝑝𝑝 𝑝𝑝𝑝𝑝𝑝𝑝 𝑏𝑏를 비교 𝑝𝑝𝑝𝑝𝑝𝑝 𝑠𝑠 = - 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 ? ? . . . ? 𝑝𝑝𝑝𝑝𝑝𝑝 = 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑐𝑐 𝑎𝑎 𝑏𝑏 𝑠𝑠 = - 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 ? ? . . . ? 𝑝𝑝𝑝𝑝𝑝𝑝 = 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑐𝑐 𝑎𝑎 𝑏𝑏
  • 62. 62 𝑗𝑗 0 1 2 3 4 5 6 7 8 9 𝑝𝑝𝑝𝑝𝑝𝑝 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑏𝑏 𝑐𝑐 𝑎𝑎 𝑐𝑐 𝑎𝑎 𝑏𝑏 𝑓𝑓 −1 −1 −1 0 1 2 3 −1 0 1
  • 63. 63 int String::FastFind(String pat) { // Determine whether pat is substring of s or not int posP = 0, posS = 0; int lengthP = pat.Length(), lengthS = Length(); while ((posP < lengthP) && (posS < lengthS)) { if (pat.str[posP] == pat.str[posS]) { // String match posP++; posS++; } else { if (posP == 0) posS++; else posP = pat.f[posP - 1] + 1; } } if (posP < lengthP) return -1; else return posS - lengthP; }
  • 64. 64
  • 65. 65
  • 66. 66 void String::FailureFunction() { // Calculate failure function about *this pattern int lengthP = Length(); f[0] = -1; for (int j = 1; j < lengthP; j++) { int i = f[j - 1]; while ((*(str + j) != *(str + i + 1)) && (i >= 0)) i = f[i]; if (*(str + j) == *(str + i + 1)) f[j] = i + 1; else f[j] = -1; } }
  • 67. 67
  • 68. 68