Rails UVa514
2018-02-10 21:36
387 查看
TImes: 17 mins.
栈混洗模板,模拟混洗过程即可。#include <iostream>
#include <stack>
using namespace std;
stack<int> A, C;
int res[1005] = { 0 };
void clear(stack<int> &x)
{
while (!x.empty())
x.pop();
}
bool solve(int N)
{
int tot = 1;
while (!A.empty())
{
if (!C.empty() && C.top() == res[tot])
{
C.pop();
tot++;
}
else
{
C.push(A.top());
A.pop();
}
}
while (!C.empty())
{
if (C.top() != res[tot])
return false;
C.pop();
tot++;
}
return true;
}
int main()
{
int N;
while (cin >> N && N)
{
while (cin >> res[1] && res[1])
{
for (int i = 2; i <= N; i++)
cin >> res[i];
clear(A); clear(C);
for (int i = N; i > 0; i--)
A.push(i);
cout << (solve(N) ? "Yes" :"No") << endl;
}
cout << endl;
}
return 0;
}
栈混洗模板,模拟混洗过程即可。#include <iostream>
#include <stack>
using namespace std;
stack<int> A, C;
int res[1005] = { 0 };
void clear(stack<int> &x)
{
while (!x.empty())
x.pop();
}
bool solve(int N)
{
int tot = 1;
while (!A.empty())
{
if (!C.empty() && C.top() == res[tot])
{
C.pop();
tot++;
}
else
{
C.push(A.top());
A.pop();
}
}
while (!C.empty())
{
if (C.top() != res[tot])
return false;
C.pop();
tot++;
}
return true;
}
int main()
{
int N;
while (cin >> N && N)
{
while (cin >> res[1] && res[1])
{
for (int i = 2; i <= N; i++)
cin >> res[i];
clear(A); clear(C);
for (int i = N; i > 0; i--)
A.push(i);
cout << (solve(N) ? "Yes" :"No") << endl;
}
cout << endl;
}
return 0;
}
相关文章推荐
- UVA 514 Rails
- UVa 514 Rails(模拟栈)
- UVA-514 Rails
- 算法竞赛入门经典 UVa 514 Rails
- UVa 514 - Rails
- Rails UVA - 514
- Uva514-Rails(基本栈)
- UVA-514 Rails (栈)
- UVa 514 Rails
- 例题6-2 铁轨(Rails, ACM/ICPC CERC 1997, UVa 514)
- Problem 038——UVa 514 - Rails
- uva-514-Rails(栈)
- UVA - 514 Rails(栈)
- uva 514 Rails
- UVA 514 铁轨 (Rails)
- UVA514 Rails(栈)
- UVa 514 - Rails
- UVa 514 Rails(stack)
- Uva Oj 514 - Rails
- UVA 514 - Rails(模拟)