# Cat CPP practice
1 2 3 5
1 3 5 1
0 0 0 0 0
0 1 0 0 0
0 0 0 1 0
0 0 0 0 2
0 1 0 0 0
```cpp=
#include<iostream>
using namespace std;
template <class T>
class Vector {
public:
class Iterator {
public:
Iterator(T* t) : _val(t) {}
T& operator*() const; // TODO: get the value of _val
Iterator& operator++(); // TODO: move the iterator to next position
// ++it
Iterator operator++(int); // TODO: move the iterator to next position
// it++
bool operator!=(const Iterator& i) const; // TODO: not equal
bool operator==(const Iterator& i) const; // TODO: equal operator
private:
T* _val;
};
Vector() : _capacity(1), _size(0) {
_data = new T[1];
}
~Vector() {
delete[] _data;
}
friend ostream& operator<<(ostream& out, const Vector& v) {
for (int i=0;i<v._size;++i) {
out << (v._data)[i] << " ";
}
return out;
}
T& operator[](int i); // TODO: set the data at index i
T operator[](int i) const; // TODO: get the data at index i
void push_back(T n); // TODO: append a number at the back, reallocate the array if needed
Iterator begin(); // TODO: the iterator of the first element
Iterator end(); // TODO: the iterator of the next element of the last element
unsigned int size() {
return _size;
}
private:
T* _data;
unsigned int _capacity;
unsigned int _size;
};
template <class T>
T& Vector<T>::Iterator::operator* () const {
return *_val;
}
template <class T>
typename Vector<T>::Iterator& Vector<T>::Iterator::operator++ () {
_val ++;
return *this;
}
template <class T>
typename Vector<T>::Iterator Vector<T>::Iterator::operator++ (int) {
_val ++;
return *this;
}
template <class T>
bool Vector<T>::Iterator::operator!= (const Iterator& i) const {
return _val != i._val;
}
template <class T>
bool Vector<T>::Iterator::operator== (const Iterator& i) const {
return _val == i._val;
}
template <class T>
T& Vector<T>::operator[](int i) {
return _data[i];
}
template <class T>
T Vector<T>::operator[](int i) const {
return _data[i];
}
template <class T>
void Vector<T>::push_back(T n) {
if (_size == _capacity){
_capacity *= 2;
T* new_data = new T[_capacity];
for (int i=0; i<_size; i++){
new_data[i] = _data[i];
}
delete[] _data;
_data = new_data;
}
_data[_size++] = n;
}
template <class T>
typename Vector<T>::Iterator Vector<T>::begin() {
Vector<T>::Iterator it(_data);
return it;
}
template <class T>
typename Vector<T>::Iterator Vector<T>::end() {
Vector<T>::Iterator it(_data + _size);
return it;
}
int main(){
Vector<int> v;
for (int i=0;i<10;i++) {
v.push_back(i);
cout << v << endl;
}
for (int i=0;i<v.size();i++) {
if (i%2 == 0) {
v[i] *= 2;
}
}
for (int i=0;i<v.size();i++) {
cout << v[i] << " ";
}
cout << endl;
for (Vector<int>::Iterator it=v.begin();it!=v.end();++it) {
cout << *it << " ";
}
cout << endl;
for (Vector<int>::Iterator it=v.begin();it!=v.end();it++) {
if (*it%3 == 0) {
*it *= 3;
}
}
cout << v << endl;
}
```
```cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
int size;
cin>>size;
int arr[size] = {0};
for (int i=0;i<size;i++){
cin>>arr[i];
}
int max = 0, gmax = 0;
int temp = 0, start = 0, end = 0;
for(int i=0;i<size;i++){
max += arr[i];
if (max<0){
max = 0;
temp = i;
}
if (max>gmax){
gmax = max;
end = i;
start = temp;
}
}
return 0;
// -1 2 -3 5 -1 2 1 -8
// gmax:
// max: 0 2 0 5 4 6 7 0 13
}
```
```cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class ScoreBonus:public Skill {
public:
ScoreBonus(int cycle, int period) { // constructor
_cycle = cycle;
_period = period;
}
int SUP(const Note note) { return 17; }
int CUP(const Note note) { return 0; }
// define or redefine something including constructor
};
class ComboBonus:public Skill {
public:
ComboBonus(int cycle, int period) { // constructor
_cycle = cycle;
_period = period;
}
int SUP(const Note note) { return 0; }
int CUP(const Note note) { return 18; }
// define or redefine something including constructor
};
class Duet:public Skill {
public:
Duet(int cycle, int period) { // constructor
_cycle = cycle;
_period = period;
}
int SUP(const Note note) { return 10; }
int CUP(const Note note) { return 15; }
// define or redefine something including constructor
};
class FlickAct:public Skill {
public:
FlickAct(int cycle, int period) { // constructor
_cycle = cycle;
_period = period;
}
int SUP(const Note note) {
if (note._type=="flick"){
return 30;
}
else{return 10;}
int CUP(const Note note) { return 0; }
// define or redefine something including constructor
};
class LongAct:public Skill {
public:
LongAct(int cycle, int period) { // constructor
_cycle = cycle;
_period = period;
}
int SUP(const Note note) {
if (note._type=="long"){
return 30;
}
else {return 10;}
int CUP(const Note note) { return 0; }
// define or redefine something including constructor
};
Note::Note(int time, string type) { // constructor
_time = time;
_type = type;
}
Party::Party(Skill* skills[]) { // constructor
for (int i=0;i<5;i++){
_skills[i] = skills[i]; //Skill(cycle, period)
}
}
int Party::calc(Note note) { // return the score of the note
//note(time, type)
// for (int i=0;i<5;i++){
// if (_skills[i].cycle - note._time < 0)
// {
// return 1000;}
int maxs = 0, maxc =0;
for ( int i=0; i< 5; i++ ){
if (note._time>_skills[i]->cycle && ((note._time-_skills[i]->cycle)/_skills[i]->_period)%2==0){
int s = _skills[i]->SUP(note);
int c = _skills[i]->CUP(note);
if (s>maxs) maxs = s;
if (c>maxc) maxc = c;
}
}
return (int)(1000.0*(1+maxs/100.0)*(1+maxc/100.0));
}
// =========================
class Note {
public:
Note(int time, string type); // constructor
int _time;
string _type; // Tap, Flick, Long
};
class Skill {
friend class Party;
public:
Skill() {}
virtual int SUP (const Note note) { return 0; }
virtual int CUP (const Note note) { return 0; }
protected:
int _cycle, _period;
};
class Party {
public:
Party(Skill* skills[]); // constructor
int calc(Note note); // return the score of the note
private:
Skill* _skills[5];
};
int main() {
Skill* vec[5];
for(int i = 0; i < 5; ++i) {
int cycle, period;
string skill;
cin >> cycle >> period >> skill;
if (skill == "score")
vec[i] = new ScoreBonus(cycle, period);
else if (skill == "combo")
vec[i] = new ComboBonus(cycle, period);
else if (skill == "duet")
vec[i] = new Duet(cycle, period);
else if (skill == "flick")
vec[i] = new FlickAct(cycle, period);
else if (skill == "long")
vec[i] = new LongAct(cycle, period);
}
Party party(vec);
int time;
string type;
while (cin >> time >> type) {
cout << party.calc(Note(time, type)) << endl;
}
return 0;
}
```
```cpp
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
char board[19][19];
/* check one sqare */
void check(int r, int c, bool is_white)
{
if (board[r][c] == '+' && board[r][c] == '*')
return;
if (board[r][c] == 'O' && is_white)
return;
if (board[r][c] == '@' && !is_white)
return;
// BFS
Q = {(r,c)}
Kill = {}
while Q is not empty:
u = Q.popfront()
u.color = BLACK
if (u == '+' || u == '*'}
return;
if (r > 0){
if (r-1, c) != board[r][c] && is WHITE:
Q.pushpack((r-1, c))
K.append((r-1, c))
(r-1, c).color = GRAY;
}
if (r < 18){
if (r+1, c) != board[r][c] is WHITE:
Q.pushpack((r+1, c))
K.append((r+1, c))
(r+1, c).color = GRAY;
}
if (c > 0){
if (r, c-1) != board[r][c] is WHITE:
Q.pushpack((r, c-1))
K.append((r, c-1))
(r, c-1).color = GRAY;
}
if (c < 18){
if (r, c+1) != board[r][c] is WHITE:
Q.pushpack((r, c+1))
K.append((r, c+1))
(r, c+1).color = GRAY;
}
}
int main()
{
int step;
cin >> step;
string *ip;
ip = new string [step];
for (int i =0;i<step;i++){
cin >> ip[i];
}
for (int i=0; i<19; i++){
for (int j=0; j<19; j++){
if (i == 3 || i == 9 || i == 15){
if (j == 3 || j == 9 || j == 15){
board[i][j] = '*';
}
}
else{
board[i][j] = '+';
}
}
}
for (int s=0; s<step; s++){
int c, r;
c = (int)ip[s][0] - (int)'A';
c = c > 8 ? c-1: c;
r = stoi(ip[s].substr(1));
bool is_white = s % 2;
if (!is_white){
board[r][c] = '@';
}
else{
board[r][c] = 'O';
}
if (r > 0){
check(r-1, c, is_white);
}
if (r < 18){
check(r+1, c, is_white);
}
if (c > 0){
check(r, c-1, is_white);
}
if (c < 18){
check(r, c+1, is_white);
}
}
}
```
0 1 2 3
1
2
3
19 + + + + + + + + + + + + + + + + + + +
18 + + + + + + + + + + + + + + + + + + +
17 + + + + + + + + + + + + + + @ + + + +
16 + + + O + + + + + O + + + + + * @ + +
15 + + + + + + + + + + + + + + + + + + +
14 + + + + + + + + + + + + + + + + + + +
13 + + + + + + + + + + + + + + + + + + +
12 + + + + + + + + + + + + + + + + + + +
11 + + + + + + + + O O O + + + + + + + O
10 + + + O + + + O @ @ O O + + + @ + O @
9 + + + + + + + O + @ @ O + + + + + O @
8 + + + + + + + + O O O + + + + + + O @
7 + + + + @ + + + + + + + + + + + + + O
6 + + + @ + @ + + + + + + + + + + + + +
5 + + + O @ O + + + O O O + + + + + + +
4 + + + + O + + + O + + O O + + @ + + +
3 + + + + + + + + O + + + O + + + + + +
2 + + + + + + + + + O O O + + + + + + +
1 + + + + + + + + + + + + + + + + + + +
A B C D E F G H J K L M N O P Q R S T
0 1 2 3 4 5 6 7 8 9
BFS breadth-first search
5
White: all
Gray:
Black: M3, L3, K3, L4, K4
Die = [M3, L3, K3, L4, K4]
Q = []
```
const int* // pointer to constant integer
const int * const // constant pointer to constant integer
int const * // pointer to constant integer
```
```
array
59 33 2
14 45 16
51 4 27
ptr_array
0x1234 0x1238 0x123c
0x1240 0x1244 0x1248
0x124c 0x1250 0x1254
temp
0x1234 0x1238 0x123c 0x1240 0x1244 ...
0x1238 0x1234
...
0 1 2 3 4 5
0*3+0 0*3+1 0*3+2 1*3+0 1*3+1 1*3+2
0,0 0,1 0,2 1,0 1,1 1,2
0 1 2 5 4 3
0*3+0 0*3+1 0*3+2 1*3+2 1*3+1 1*3+0
0 1 2 3 4 5
0,1 0,1 0,2 1,2 1,1 1,0
for(int i=0; i<m*m; i++){
int y;
int x = i / m;
if(x % 2 != 0){
y = (m-1) - i % m;
}
else {
y = i % m;
}
ptr_array[x][y] = temp[i];
}
for(int i=0; i<m; i++){
for(int j=0; j<m; j++){
int k = m * i + (m-1) - j;
temp[k] = ptr_array[i][j];
}
}
```
``` cpp
using namespace std;
void snake(const int *ptr_array[100][100], int m) {
// TODO
}
```
``` cpp
1 0
* 1 2 3
-------
1 1
1 1
1 1
-------
1 2 2 1
0: 0*0
1: 1*0 + 0*1
2: 2*0 + 1*1
3: 2*1
===========================
int len = (p[0][0] + 1) * (p[0][1] + 1)
coe = [1 1 1 1]
pow = [0 1 1 2]
ans = [1 2 1]
for (int i=0; i<len; i++){
int c = coe[i];
int p = pow[i];
ans[p] += c;
}
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int main(){
string s[2];
cin >> s[0] >> s[1];
// the order of the polynomial
int order[2];
std::vector<int> coe[2];
for(int i=0; i<2; i++){
int n1 = 0, n2 = 0, c, e;
// std::find returns the position of the first matching string
// https://en.cppreference.com/w/cpp/string/basic_string/find
n1 = s[i].find("x^");
n2 = s[i].find("+");
// std::substr returns a slice of the string; std::stoi converts string to int
// https://en.cppreference.com/w/cpp/string/basic_string/substr
order[i] = stoi( s[i].substr(n1+2, n2-n1-2) );
cout << "\norder " << order[i] << endl;
// resize the coefficient vector to the correct order, and set all values to 0
coe[i].resize(order[i]+1);
coe[i][order[i]] = stoi( s[i].substr(0, n1) );
// store the coefficient in the vector
cout << stoi( s[i].substr(0, n1) ) << "x^" << order[i] << "+";
while (s[i].find("x^", n1+1) != std::string::npos){
n1 = s[i].find("x^", n1+1);
c = stoi( s[i].substr(n2+1, n1-n2-1) );
n2 = s[i].find("+", n2+1);
if (n2 != std::string::npos) {
e = stoi( s[i].substr(n1+2, n2-n1-2) );
} else {
e = stoi( s[i].substr(n1+2) );
}
coe[i][e] = c;
cout << c << "x^" << e << "+";
}
if (n2 != std::string::npos){
coe[i][0] = stoi( s[i].substr(n2) );
cout << stoi( s[i].substr(n2) );
}
cout << endl;
for (int j=0; j<order[i]+1; j++){
cout << coe[i][j] << " ";
}
cout << endl;
}
}
```
``` c++
1 2 3
4 5 6
7 8 9
mat[1][0]
#include <iostream>
#include <vector>
using namespace std;
int main(){
int size, k, x, y;
cin >> size >> k >> x >> y;
// vector<int> one_d_array(5, 0);
vector< vector<int> > mat(size, vector<int>(size, 0));
// down:0, right:1, up:2, left:3
int dir, prev_dir;
int count = 1;
mat[x][y] = count;
count++;
if(k==1){
dir = 1;
prev_dir = 1;
while(1){
switch(dir){
case 0:
x = (x + 1) % size;
if (prev_dir == 1){
dir = 3;
pre_dir = 3;
}
if (prev_dir == 3){
pre_dir = dir = 1;
}
break;
case 1:
if (y == size-1){
dir = 0;
continue;
} else y++;
break;
case 3:
if (y == 0){
dir = 0;
continue;
} else y--;
break;
}
if(mat[x][y]){
break;
}
mat[x][y] = count;
count++;
}
}
else{
}
}
```