码迷,mamicode.com
首页 > 其他好文 > 详细

SGU 191.Exhibition(模拟)

时间:2014-10-13 21:41:07      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

时间限制:0.25s

空间限制:4M

题意:

       有两个公司A、B,他们要展览物品,但是A公司的展柜要放B公司的物品,B公司的展柜要放A公司物品。最开始只有一个空柜台,从指定的一个公司开始,轮流进行操作,可选的操作有两个:①选一个自己公司的空展柜放上对方公司的物品         ②选一个自己公司的空展柜,在这个展柜左边插入一个对方公司的空展柜,再在这个新插入的展柜左边插入一个空展柜并放上自己公司的物品。

题中给出了一个最终物品摆放的序列,问最后物品摆放的序列能否和给定序列相同。

 

 

 

 


 

Solution:

               对任意一个空展台的操作是影响不了前面的已经放置好了的展台的,所有,只要从左往右依次判断能否放,并用栈模拟操作就好了。

 

 

code

 

bubuko.com,布布扣
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
string s, c;
stack<int> tem;
int main() {
    ios::sync_with_stdio (0);
    cin >> c >> s;
    int init = (c[0] == B);
    int  now, i, t = 1;
    tem.push (init);
    for (i = 0; i < s.size(); i++) {
        if (!tem.empty() )   now = tem.top();
        else
            break;
        if (s[i] == A) {
            if (now == 1)  tem.pop(), --t;
            else tem.push (now ^ 1), ++t;
        }
        else if (s[i] == B) {
            if (now == 0)  tem.pop(), --t;
            else tem.push (now ^ 1), ++t;
        }
    }
    if (i == s.size() && t == 0) puts ("YES");
    else   puts ("NO");
}
View Code

 

SGU 191.Exhibition(模拟)

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/keam37/p/4022982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!