Stacks problem questions

Problems based on Stacks


  • UVa 00127 - “Accordian” Patience
  • UVa 00514 - Rails *
  • UVa 00732 - Anagram by Stack *
  • UVa 01062 - Containers *
  • UVa 10858 - Unique Factorization
Here are my solution and hints to some of them:

Uva 514: Rails

You just need to do basic simulation. A little bit of thought and you'll be able to make it.
Here is the code that I wrote for the problem.
#include <bits/stdc++.h>
using namespace std;
///Uva 514 Rails
int main(int argc, char const *argv[]) {
int N;
while (scanf("%d", &N)!=EOF&&N!=0) {
int first;
while (scanf("%d", &first), first!=0) {
int permutation[N];
permutation[0]=first;
for (size_t i = 1; i < N; i++) {
scanf("%d", &permutation[i]);
}
int end=0;
stack<int> s;
bool canBe = true;
for(int i=1;i<=N;i++){
while (!s.empty()&&(s.top()==permutation[end])) {
end++;
s.pop();
}
if(i==permutation[end]&&i!=N){
end++;
}else if(i<permutation[end]&&i<N){
s.push(i);
}else if(i>permutation[end]&&i<N){
//////////failure
canBe=false;
}else if(i==N){
s.push(i);
while(!s.empty()){
if(permutation[end]!=s.top()){
//////////failure
canBe=false;
break;
}
s.pop();
end++;
}
}
if(!canBe)
break;
}
if(canBe)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
cout<<'\n';
}
return 0;
}

No comments:

Post a Comment

Installing albert on ubuntu 19.04

Installing Albert on Ubuntu 19.04... Albert is not still released for ubuntu 19.04. But still, you can install it using the following ...