Skip to content

Commit

Permalink
stack problem
Browse files Browse the repository at this point in the history
  • Loading branch information
MrinmoiHossain committed Aug 16, 2019
1 parent cd41175 commit e7d0e73
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Data Structures/Stacks/EqualStacks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <bits/stdc++.h>
using namespace std;

int height(stack<int> a, stack<int> b, stack<int> c)
{
while(1){
if(a.empty() || b.empty() || c.empty())
return 0;

int atop = a.top(), btop = b.top(), ctop = c.top();
//cout << atop << endl;
if(atop == btop && btop == ctop)
return atop;

if(atop >= btop && atop >= ctop)
a.pop();
else if(btop >= atop && btop >= ctop)
b.pop();
else if(ctop >= atop && ctop >= btop)
c.pop();

}
}

stack<int> input(int n)
{
vector<int> v(n);
for(int i = 0; i < n; i++)
cin >> v[i];

stack<int> s;
int sm = 0;
for(int i = n - 1; i >= 0; i--){
sm += v[i];
s.push(sm);
}

return s;
}

int main(void)
{
int a, b, c;
cin >> a >> b >> c;

stack<int> as = input(a), bs = input(b), cs = input(c);

cout << height(as, bs, cs) << endl;

return 0;
}
37 changes: 37 additions & 0 deletions Data Structures/Stacks/SimpleTextEditor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;

int main(void)
{
int t;
cin >> t;

stack<string> s;
string ss;
for(int i = 0, n, k; i < t; i++){
cin >> n;
string st;

if(n == 1){
cin >> st;
s.push(ss);
ss += st;
}
else if(n == 2){
cin >> k;
s.push(ss);
ss.erase(ss.size() - k);
}
else if(n == 3){
cin >> k;
cout << ss[k-1] << endl;
}
else if(n == 4){
ss = s.top();
s.pop();
}
}

return 0;
}

71 changes: 71 additions & 0 deletions Data Structures/Stacks/Waiter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <bits/stdc++.h>
#define MAX 11002
using namespace std;

vector<int> prime;
bool primecheck[MAX];

void primeFunction(void)
{
int limit = sqrt(MAX);

primecheck[0] = 1, primecheck[1] = 1;
for(int i = 4; i <= MAX; i += 2)
primecheck[i] = 1;

prime.push_back(2);
for(int i = 3; i <= MAX; i += 2){
if(!primecheck[i]){
prime.push_back(i);

if(i <= limit){
for(int j = i * i; j <= MAX; j += i * 2)
primecheck[j] = 1;
}
}
}
}

int main(void)
{
primeFunction();
//cout << v.size() << endl;
int n, q;
cin >> n >> q;

stack<int> s;
for(int i = 0, a; i < n; i++){
cin >> a;
s.push(a);
}

int con = 0;
stack<int>v[1201];
while(con < q){
int p = prime[con];
stack<int> temp;
while(!s.empty()){
if(s.top() % p == 0)
v[con].push(s.top());
else
temp.push(s.top());
s.pop();
}
s = temp;
con++;
}

for(int i = 0; i < q; i++){
while(!v[i].empty()){
cout << v[i].top() << endl;
v[i].pop();
}
}

while(!s.empty()){
cout << s.top() << endl;
s.pop();
}

return 0;
}

0 comments on commit e7d0e73

Please sign in to comment.