SlideShare a Scribd company logo
Data Structure - 2nd Study
Data Structure - 2nd Study
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
Data Structure - 2nd Study
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
Data Structure - 2nd Study
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 (20)

DOCX
Travel management
1Parimal2
 
PDF
Static and const members
mohamed sikander
 
PDF
C++ TUTORIAL 8
Farhan Ab Rahman
 
PDF
Stl algorithm-Basic types
mohamed sikander
 
PDF
C++ TUTORIAL 5
Farhan Ab Rahman
 
PDF
Implementing string
mohamed sikander
 
DOCX
Basic Programs of C++
Bharat Kalia
 
PDF
Implementing stack
mohamed sikander
 
PPT
Oop1
Vaibhav Bajaj
 
DOCX
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Alex Penso Romero
 
PDF
2. Базовый синтаксис Java
DEVTYPE
 
PDF
Operator overloading
mohamed sikander
 
PDF
9. pointer, pointer & function
웅식 전
 
PDF
Inheritance and polymorphism
mohamed sikander
 
PDF
When RV Meets CEP (RV 2016 Tutorial)
Sylvain Hallé
 
PDF
C++ TUTORIAL 6
Farhan Ab Rahman
 
PDF
Nesting of for loops using C++
prashant_sainii
 
PDF
C++ TUTORIAL 2
Farhan Ab Rahman
 
PDF
C++ TUTORIAL 1
Farhan Ab Rahman
 
Travel management
1Parimal2
 
Static and const members
mohamed sikander
 
C++ TUTORIAL 8
Farhan Ab Rahman
 
Stl algorithm-Basic types
mohamed sikander
 
C++ TUTORIAL 5
Farhan Ab Rahman
 
Implementing string
mohamed sikander
 
Basic Programs of C++
Bharat Kalia
 
Implementing stack
mohamed sikander
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Alex Penso Romero
 
2. Базовый синтаксис Java
DEVTYPE
 
Operator overloading
mohamed sikander
 
9. pointer, pointer & function
웅식 전
 
Inheritance and polymorphism
mohamed sikander
 
When RV Meets CEP (RV 2016 Tutorial)
Sylvain Hallé
 
C++ TUTORIAL 6
Farhan Ab Rahman
 
Nesting of for loops using C++
prashant_sainii
 
C++ TUTORIAL 2
Farhan Ab Rahman
 
C++ TUTORIAL 1
Farhan Ab Rahman
 

Viewers also liked (12)

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

Similar to Data Structure - 2nd Study (20)

PPT
Sparse Matrix and Polynomial
Aroosa Rajput
 
PDF
I wrote the following change it to having a header, main and cpp fi.pdf
rishteygallery
 
PPT
Array
Malainine Zaid
 
PDF
#include iostream using namespace std; class Array { priva.pdf
ANSAPPARELS
 
PPTX
1.Array and linklst definition
balavigneshwari
 
DOCX
Cpp
Ankit Dubey
 
PPT
Arrays
Komal Singh
 
PPT
Data structures KTU chapter2.PPT
Albin562191
 
PDF
I have to write a polynomial class linked list program and i do not .pdf
jibinsh
 
DOC
Experiment 06 psiplclannguage expermient.doc
anuharshpawar22
 
PDF
C++ normal assignments by maharshi_jd.pdf
maharshi1731
 
DOCX
cosc 281 hw3
Brian Goggins
 
PPT
Chapter2
Nashra Akhter
 
PDF
First C++ code written... I think its quite efficient with all the.pdf
ankit482504
 
PDF
Assignment on Numerical Method C Code
Syed Ahmed Zaki
 
PDF
jacobi method, gauss siedel for solving linear equations
Department of Telecommunications, Ministry of Communication & IT (INDIA)
 
DOCX
2 d matrices
Himanshu Arora
 
PDF
#include -string- #include -string- #include -vector- #include -iostre (1).pdf
ashiyanabakersandcon
 
Sparse Matrix and Polynomial
Aroosa Rajput
 
I wrote the following change it to having a header, main and cpp fi.pdf
rishteygallery
 
#include iostream using namespace std; class Array { priva.pdf
ANSAPPARELS
 
1.Array and linklst definition
balavigneshwari
 
Arrays
Komal Singh
 
Data structures KTU chapter2.PPT
Albin562191
 
I have to write a polynomial class linked list program and i do not .pdf
jibinsh
 
Experiment 06 psiplclannguage expermient.doc
anuharshpawar22
 
C++ normal assignments by maharshi_jd.pdf
maharshi1731
 
cosc 281 hw3
Brian Goggins
 
Chapter2
Nashra Akhter
 
First C++ code written... I think its quite efficient with all the.pdf
ankit482504
 
Assignment on Numerical Method C Code
Syed Ahmed Zaki
 
jacobi method, gauss siedel for solving linear equations
Department of Telecommunications, Ministry of Communication & IT (INDIA)
 
2 d matrices
Himanshu Arora
 
#include -string- #include -string- #include -vector- #include -iostre (1).pdf
ashiyanabakersandcon
 
Ad

More from Chris Ohk (20)

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

Recently uploaded (20)

PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PDF
How Current Advanced Cyber Threats Transform Business Operation
Eryk Budi Pratama
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
Bitcoin+ Escalando sin concesiones - Parte 1
Fernando Paredes García
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Arcee AI - building and working with small language models (06/25)
Julien SIMON
 
PDF
Upskill to Agentic Automation 2025 - Kickoff Meeting
DianaGray10
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PDF
Shuen Mei Parth Sharma Boost Productivity, Innovation and Efficiency wit...
AWS Chicago
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
How Current Advanced Cyber Threats Transform Business Operation
Eryk Budi Pratama
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Bitcoin+ Escalando sin concesiones - Parte 1
Fernando Paredes García
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Arcee AI - building and working with small language models (06/25)
Julien SIMON
 
Upskill to Agentic Automation 2025 - Kickoff Meeting
DianaGray10
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Top Managed Service Providers in Los Angeles
Captain IT
 
Shuen Mei Parth Sharma Boost Productivity, Innovation and Efficiency wit...
AWS Chicago
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 

Data Structure - 2nd Study

  • 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
  • 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
  • 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