Stack & Queue
stack = stream with only ome input/output port
stack = stream with two separated input port, output post
stack in/
s out
queue out in
q
Methods
vector<T> v; // khoi tao
[Link]();
v.push_back(x); // nap x vao dau phai
v.push_back([Link](), x); // nap x vao dau trai
v.push_back([Link]()+p, x); // xen x vao truoc vi tri p
[Link](x); // xem dau phai
[Link](); // xem dau trai
v.pop_back(); // xoa dau phai
[Link]([Link]()-1); // xoa dau phai
[Link]([Link]()); // xoa dau trai
sort([Link](), [Link]());
Hien thi
for(int i = 0; i < [Link](); ++i) cout << " " << v[i];
VI w(v); // VI w; copy v -> w: Tao moi va gan tri
[Link](v); // copy v -> w
[Link](); // xoa noi dung van con bien v
[Link]();
Method Lef Right
t
vector VI v
clear [Link]()
size
print
begin
end
add
xem
xoa
Demo
vector vector Library
stack in: [Link](x);
s s.push_back(x)
out: x = x =
[Link](); [Link]();
[Link]([Link]()-
1)
return x;
queue out: x = in: [Link](x);
q [Link](); q.push_back(x)
x =
[Link]([Link]()) [Link]();
return x;
Stack demo 1
/*
Name: stack
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description:
Tinh tri cua bieu thuc hau to (Lukasiewicz)
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
void Go() {
cout << " ? ";
fflush(stdin);
if ([Link]()=='.') exit(0);
}
void Print(VI v, const char *msg = "") {
cout << msg;
for(int i = 0; i < [Link](); ++i)
cout << " " << v[i];
}
void Run() {
VI s;
for(int x = 1; x < 10; ++x)
s.push_back(x);
Print(s);
for(int i = 1; i <= 3; ++i) {
int x = [Link]();
cout << "\n i = " << i << ". x = " << x;
[Link]([Link]()-1);
}
Print(s, "\n s: ");
}
main() {
Run();
cout << endl << "\n\n T h e E n d \n";
return 0;
}
Output 1
1 2 3 4 5 6 7 8 9
i = 1. x = 9
i = 2. x = 8
i = 3. x = 7
s: 1 2 3 4 5 6
Stack Demo 2
/*
Name: stack
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description:
Dung stack lat mang a[d..c]
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> Stack;
#define Push(s,x) s.push_back(x)
void Go() {
cout << " ? ";
fflush(stdin);
if ([Link]()=='.') exit(0);
}
void Print(Stack v, const char *msg = "") {
cout << msg;
for(int i = 0; i < [Link](); ++i)
cout << " " << v[i];
}
void Print(int a[], int n, const char *msg = "") {
cout << msg;
for(int i = 0; i < n; ++i)
cout << " " << a[i];
}
int Pop(Stack &s) {
int x = [Link]();
[Link]([Link]()-1);
return x;
}
// lat mang tu a[d..c]
void Rev(int a[], int d, int c) {
Stack s;
for(int i = d; i <= c; ++i)
Push(s,a[i]);
Print(s, "\n stack s: ");
for(int i = d; i <= c; ++i)
a[i] = Pop(s);
Print(s, "\n stack s = empty: ");
}
void Run() {
int a[] = {0,1,2,3,4,5,6,7,8,9};
int n = sizeof(a) / sizeof(int);
Print(a, n, "\n Init a: ");
Rev(a, 3, 6);
Print(a, n, "\n Rev(a[3..6]): ");
Rev(a, 0, n-1);
Print(a, n, "\n Rev(a[0..n-1]): ");
}
main() {
Run();
cout << endl << "\n\n T h e E n d \n";
return 0;
}
Output 2
Init a: 0 1 2 3 4 5 6 7 8 9
stack s: 3 4 5 6
stack s = empty:
Rev(a[3..6]): 0 1 2 6 5 4 3 7 8 9
stack s: 0 1 2 6 5 4 3 7 8 9
stack s = empty:
Rev(a[0..n-1]): 9 8 7 3 4 5 6 2 1 0
T h e E n d
Stack Demo3
/*
Name: stack
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description:
Tinh tri cua bieu thuc hau to (Lukasiewicz)
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> Stack;
#define Push(s,x) s.push_back(x)
void Go() {
cout << " ? ";
fflush(stdin);
if ([Link]()=='.') exit(0);
}
void Print(Stack v, const char *msg = "") {
cout << msg;
for(int i = 0; i < [Link](); ++i)
cout << " " << v[i];
}
void Print(int a[], int n, const char *msg = "") {
cout << msg;
for(int i = 0; i < n; ++i)
cout << " " << a[i];
}
int Pop(Stack &s) {
int x = [Link]();
[Link]([Link]()-1);
return x;
}
void Run() {
// a bcdef ghijk lmnop qrstu vwxyz
// e = "(k+f)*(j+b)"; // (10+5)*(9+1) = 15*10 = 150
string h = "kf+jb+*"; // Hau to
cout << "\n (k+f)*(j+b) -> " << h;
Stack s;
int a, b;
for(int i = 0; i < [Link](); ++i) {
char c = h[i];
if(c >= 'a' && c <= 'z') {
Push(s,c-'a');
continue;
}
switch(c) {
case '+': a = Pop(s); b = Pop(s);
Push(s,b+a);
break;
case '-': a = Pop(s); b = Pop(s);
Push(s,b-a);
break;
case '*': a = Pop(s); b = Pop(s);
Push(s,b*a);
break;
case '/': a = Pop(s); b = Pop(s);
Push(s,b/a);
break;
} // switch
} // for
cout << "\n Final result: " << Pop(s);
}
main() {
Run();
cout << endl << "\n\n T h e E n d \n";
return 0;
}
Output 3
(k+f)*(j+b) -> kf+jb+*
Final result: 150
T h e E n d
Joshephus Problem (game)
Rounding of n elements, count k elem. Who is the rest ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
m = 14, k = 3, res = 2
Algorithm? Data Structure?
5 approaches
Version 1. rounding scan (rai gianh) int array
Version 2. rounding scan (rai gianh) bit array
Version 2. rounding scan (rai gianh) bit array
/*
Name: [Link]
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description: Josephus Problem
'''
*/
#include <bits/stdc++.h>
using namespace std;
void Go() {
cout << " ? ";
fflush(stdin);
if ([Link]()=='.') exit(0);
}
void Print(int a[], int d, int c, const char * msg = "") {
cout << msg;
for (int i = d; i <= c; ++i) {
cout << " " << a[i];
}
}
void Print(char b[], int d, int c, const char * msg = "") {
cout << msg;
for (int i = d; i <= c; ++i) {
cout << " " << (int)b[i];
}
}
// int array, mark 0/1 O(n)
int JP1(int n, int k) {
int a[n];
for(int i = 0; i < n; ++i) a[i] = i+1;
int n1 = n-1;
Print(a, 0, n1, "\n Init");
int d, j = -1;
for(int i = 1; i < n; ++i) {
d = 0;
while(d < k) {
j = (j + 1) % n;
if(a[j] > 0) ++d;
}
a[j] = 0;
Print(a, 0, n1, "\n a:");
} // i
Print(a, 0, n-1, "\n Rest:");
int res;
for(int i = 0; i < n; ++i)
if (a[i] > 0) {
res = a[i];
break;
}
cout << "\n Result: " << res;
return res;
}
// int array, counting O(n)
int JP2(int n, int k) {
int a[n];
for(int i = 0; i < n; ++i) a[i] = 1;
int n1 = n-1;
Print(a, 0, n1, "\n Init");
int d, j = -1;
for(int i = 1; i < n; ++i) {
d = 0;
while(d < k) {
j = (j + 1) % n;
d += a[j];
}
a[j] = 0;
Print(a, 0, n1, "\n a:");
} // i
Print(a, 0, n-1, "\n Rest:");
int res;
for(int i = 0; i < n; ++i)
if (a[i] > 0) {
res = i+1;
break;
}
cout << "\n Result: " << res;
return res;
}
// bit array, O(n)
int JP3(int n, int k) {
char a[n];
for(int i = 0; i < n; ++i) a[i] = 1;
int n1 = n-1;
Print(a, 0, n1, "\n Init");
int d, j = -1;
for(int i = 1; i < n; ++i) {
d = 0;
while(d < k) {
j = (j + 1) % n;
d += a[j];
}
a[j] = 0;
Print(a, 0, n1, "\n a:");
} // i
Print(a, 0, n-1, "\n Rest:");
int res;
for(int i = 0; i < n; ++i)
if (a[i] > 0) {
res = i+1;
break;
}
cout << "\n Result: " << res;
return res;
}
void Print(vector<int> a, const char * msg = "") {
cout << msg;
for (int i = 0; i < [Link](); ++i) {
cout << " " << a[i];
}
}
// Queue O(n)
int JP4(int n, int k) {
vector<int> q;
for(int i = 1; i <= n; ++i)
q.push_back(i);
Print(q, "\n Init: ");
for(int i = 1; i < n; ++i) { // lap n-1 lan
// chuyen k-1 phan tu dau ve cuoi
for(int j = 1; j < k; ++j) {
q.push_back([Link]()); // lay dau
[Link]([Link]()); // bo dau
} // j
[Link]([Link]()); // bo phan tu thu k
Print(q, "\n q: "); //Go();
} // i
int res = [Link]();
cout << "\n Result: " << res;
return res;
}
void Rev(int a[], int d, int c) {
while(d < c) {
int x = a[d]; a[d] = a[c]; a[c] = x;
++d; --c;
}
}
// 123|4567 -> 4567|123
// 321|7654 -> 4567|123
// chuyen m phan tu ve cuoi
// 3n
void Move(int a[], int n, int m) {
Rev(a, 0, m-1);
Rev(a, m, n-1);
Rev(a, 0, n-1);
}
// Myqeueu, O(n) Tu cai queue
int JP5(int n, int k) {
int a[n];
for(int i = 0; i < n; ++i) a[i] = i+1;
Print(a, 0, n-1, "\n Init: ");
int nn = n;
for(int i = 1; i < nn; ++i) { // lap n-1 lan
Move(a, n, k);
--n;
Print(a, 0, n-1, "\n a: ");
}
return a[0];
}
main() {
int n = 14, k = 3;
JP5(n,k);
cout << endl << "\n\n T h e E n d \n";
return 0;
}
If you want...Ver 6
/*
Name: [Link] (Ver. 6)
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description: Josephus Problem
'''
*/
#include <bits/stdc++.h>
using namespace std;
/*
vector<T> q;
q.push_back(x); // nap x vao dau phai
x = [Link](); // xem dau trai
[Link]([Link]()); // xoa dau trai
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
*/
#define Add Push
#define AddRight Push
#define GetLeft Get
#define Del Erase
#define Delete Erase
#define Front Get
class MyQueue {
public:
vector<int> Data;
MyQueue() { [Link](); }
inline void Push(int x) { // add x to the right
Data.push_back(x);
}
inline Erase() { // delete elem in the left
[Link]([Link]());
}
inline int TakeOut() { // takout lay phan tu tu dau trai
int x = [Link](); // lay tri
Erase(); // xoa
}
inline int Get() { // xem dau trai
return [Link]();
}
inline void Move() { // lay trai -> nap phai
int x = [Link]();
Push(x);
[Link]([Link]());
//Push(Take());
}
void Print(const char * msg) {
cout << msg;
for (int i = 0; i < [Link](); ++i) {
cout << " " << Data[i];
}
}
};
void Go() {
cout << " ? ";
fflush(stdin);
if ([Link]()=='.') exit(0);
}
void Print(int a[], int d, int c, const char * msg = "") {
cout << msg;
for (int i = d; i <= c; ++i) {
cout << " " << a[i];
}
}
void Print(char b[], int d, int c, const char * msg = "") {
cout << msg;
for (int i = d; i <= c; ++i) {
cout << " " << (int)b[i];
}
}
void Print(vector<int> a, const char * msg = "") {
cout << msg;
for (int i = 0; i < [Link](); ++i) {
cout << " " << a[i];
}
}
// Using Queue
int JP4(int n, int k) {
MyQueue q;
for(int i = 1; i <= n; ++i) [Link](i);
[Link]("\n Init: ");
for(int i = 1; i < n; ++i) { // lap n-1 lan
// chuyen k-1 phan tu dau ve cuoi
for(int j = 1; j < k; ++j) [Link]();
[Link](); // bo phan tu thu k
[Link]("\n q: "); //Go();
} // i
int res = [Link]();
cout << "\n Result: " << res;
return res;
}
main() {
int n = 14, k = 3;
JP4(n,k);
cout << endl << "\n\n T h e E n d \n";
return 0;
}
One more...Ver 7: Simplest queue
0 1 2 3 4 5 6 7 8 9 1 1 1 1
0 1 2 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
L R
/*
Name: [Link] (Ver 7)
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description: Josephus Problem
simplest queue
'''
*/
#include <bits/stdc++.h>
using namespace std;
int L, R, N;
int *a;
void Go() {
cout << " ? ";
fflush(stdin);
if ([Link]()=='.') exit(0);
}
inline void Inc(int &i) { i = (i+1) % N; }
void Print(int a[], int d, int c, const char * msg = "") {
cout << msg;
while(true) {
cout << " " << a[d];
if (d == c) return;
Inc(d);
}
}
int TakeOut() { // TakeOut the left
int x = a[L];
Inc(L);
return x;
}
void Push(int x) { // add x to the Rigjht
Inc(R);
a[R] = x;
}
void Move() { // Move L -> R
int x = TakeOut();
Push(x);
}
// Using simplest Queue
int JP7(int n, int k) {
N = n;
a = new int[N];
//fill(a, a+N, 0);
L = 0; R = N-1;
for(int i = 0; i < n; ++i) a[i] = i + 1;
Print(a, L, R, "\n Init: ");
for(int i = 1; i < N; ++i) { // loop n-1 times
// move k-1 elem front L to R
for(int j = 1; j < k; ++j) Move();
TakeOut(); // del
Print(a, L, R, "\n a: ");
}
int res = a[L];
cout << "\n Result: " << res;
return res;
}
main() {
int n = 14, k = 3;
JP7(n,k);
cout << endl << "\n\n T h e E n d \n";
return 0;
}
MyQueue Again
/*
Name: [Link] (Ver 8)
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description: Josephus Problem
simplest queue
'''
*/
#include <bits/stdc++.h>
using namespace std;
void Go() {
cout << " ? ";
fflush(stdin);
if ([Link]()=='.') exit(0);
}
class MyQueue {
public:
// Data
int * Data;
int Len;
int L, R;
// Methods
inline MyQueue(int n) {
Len = n;
Data = new int[Len];
fill(Data, Data + Len, 0);
L = 0; R = Len-1;
}
inline Size() { return Len; }
inline void Inc(int &i) { i = (i+1) % Len; }
inline void Print(const char * msg = "") {
int d = L, c = R;
cout << msg;
while(true) {
cout << " " << Data[d];
if (d == c) return;
Inc(d);
}
}
inline int TakeOut() { // TakeOut the left
int x = Data[L];
Inc(L);
return x;
}
inline void Push(int x) { // add x to the Rigjht
Inc(R);
Data[R] = x;
}
inline void Move() { // Move L -> R
Push(TakeOut());
}
inline int Left() {
return Data[L];
}
inline int Right() {
return Data[R];
}
};
// Using simplest Queue
int JP7(int n, int k) {
MyQueue q(n);
for(int i = 0; i < n; ++i)
[Link](i+1);
[Link]("\n Init: "); Go();
for(int i = 1; i < n; ++i) { // loop n-1 times
// move k-1 elem front L to R
for(int j = 1; j < k; ++j) [Link]();
[Link](); // del
[Link]("\n q: ");
}
int res = [Link]();
cout << "\n Result: " << res;
return res;
}
main() {
int n = 14, k = 3;
JP7(n,k);
cout << endl << "\n\n T h e E n d \n";
return 0;
}
Using stringstream
/*
Name: [Link] (Ver 9)
Copyright: (C) 2023
Author: Devcpp Fan
Date: 22-06-23 11:50
Description: Josephus Problem
stringstream
'''
*/
#include <bits/stdc++.h>
using namespace std;
void Go() {
cout << " ? ";
fflush(stdin);
if ([Link]()=='.') exit(0);
}
// Using simplest Queue
int JP9(int n, int k) {
stringstream ss;
char BL = ' '; // 32
for(int i = 1; i <= n; ++i) {
ss << i; ss << BL;
}
int x;
for(int i = 1; i < n; ++i) { // loop n-1 times
for(int j = 1; j < k; ++j) {
ss >> x;
ss << BL << x;
}
ss >> x;
cout << "\n " << i << ": " << [Link]();
}
int res;
ss >> res;
cout << "\n Result: " << res;
return res;
}
main() {
int n = 14, k = 3;
JP9(n,k);
cout << endl << "\n\n T h e E n d \n";
return 0;
}