D. Alternating Current
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting
right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again.

The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some
order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view):

Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and
without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut.

To understand the problem better please read the notes to the test samples.

Input

The single line of the input contains a sequence of characters "+" and "-" of length
n (1 ≤ n ≤ 100000). The
i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the
i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise.

Output

Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled.

Sample test(s)
Input
-++-
Output
Yes
Input
+-
Output
No
Input
++
Output
Yes
Input
-
Output
No
Note

The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses.

In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled:

In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher:

In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself:

题目链接:http://codeforces.com/problemset/problem/344/D

题目大意:有两根导线(+线和-线)连接着电源和移动设备,+表示+线在 - 线上面。- 表示 - 线在+线上面。它们缠绕在一起,求能否在不移动电源和设备的条件下把两根线分开。

解题思路:栈模拟。一開始用搜索写超时了,后来才知道是一题脑洞题,堆栈模拟就能做出来。观察发现导线相交点为奇数个时一定不能分开。由于终于会有至少一个点相交,而要分开的条件是相交点两两相应相等,用栈实现就是:若当前点与栈顶元素相等,则它们是相应的,抛出。反之,不是相应的,压入。等待推断下一个。

代码例如以下:

#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
const int maxn=1000005;
char s[maxn];
int n;
int main(void)
{
scanf("%s",s);
n=strlen(s);
if(n%2)
{
printf("No\n");
return 0;
}
stack<char>st;
for(int i=0;i<n;i++)
{
if(!st.size()) //假设栈里没有元素,压入字符
st.push(s[i]);
else
{
char t=st.top();
if(t==s[i]) //相等,则它们是相应的,此处导线可分开抛出。 st.pop();
else //不是相应的,压入,等待推断下一个。
st.push(s[i]);
}
}
if(!st.size())
printf("Yes\n");
else
printf("No\n");
}

Codeforces Round #200 (Div. 2)D. Alternating Current (堆栈)的更多相关文章

  1. Codeforces Round #200 (Div. 1) B. Alternating Current 栈

    B. Alternating Current Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...

  2. Codeforces Round #200 (Div. 1 + Div. 2)

    A. Magnets 模拟. B. Simple Molecules 设12.13.23边的条数,列出三个等式,解即可. C. Rational Resistance 题目每次扩展的电阻之一是1Ω的, ...

  3. Codeforces Round #200 (Div. 1) C. Read Time 二分

    C. Read Time Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/C ...

  4. Codeforces Round #200 (Div. 1)A. Rational Resistance 数学

    A. Rational Resistance Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...

  5. Codeforces Round #200 (Div. 1)D. Water Tree dfs序

    D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...

  6. Codeforces Round #200 (Div. 2) C. Rational Resistance

    C. Rational Resistance time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. Codeforces Round #200 (Div. 1) BCD

    为了锻炼个人能力奋力div1 为了不做原题从200开始 B 两个电线缠在一起了 能不能抓住两头一扯就给扯分开 很明显当len为odd的时候无解 当len为偶数的时候 可以任选一段长度为even的相同字 ...

  8. Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)

    思路: dfs序其实是很水的东西.  和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...

  9. Codeforces Round #200 (Div. 2) E. Read Time(二分)

    题目链接 这题,关键不是二分,而是如果在t的时间内,将n个头,刷完这m个磁盘. 看了一下题解,完全不知怎么弄.用一个指针从pre,枚举m,讨论一下.只需考虑,每一个磁盘是从右边的头,刷过来的(左边来的 ...

随机推荐

  1. poj1958-汉诺四塔问题(三种方法)

    链接:http://poj.org/problem?id=1958 大意:汉诺塔升级版,四根柱子,n个盘子,求最少移动次数: 两种方法 递推or递归(当然还有思路3--打表) 思路1:递推(或者DP? ...

  2. python 4:str.lstrip()、str.rstrip()、str.strip()(分别去除首空格,尾空格,首尾空格;不改变原有变量,除非赋给)

    name = " Hello,World! Hello,Python! " print(name + "检测行末空格的") print(name.lstrip( ...

  3. swift-自定义TabBar工具栏

    class EBTAppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(ap ...

  4. HTML+CSS(11)

    n  CSS背景属性 Background-color:背景色. Background-image:背景图片地址.如:background-image:url(images/bg.gif;) Back ...

  5. Android 权限管理(持续整理)

    1. Android 6.0之后,APP可以直接安装,运行时再询问用户授予相关权限,此时系统弹出一个对话框,(这个对话框不能由开发者定制) 同时用户也可以在手机的“设置”中对于某个App进行权限管理 ...

  6. Android 存储路径选择

    Android能用来存储的地方有两个,一个是手机内置的存储空间,一个是外置的SD卡,内置的存储空间一般比较小,所以应用的缓存建议存储在外置的SD卡中. 在Android系统中如何获得存储的路径呢? p ...

  7. angular js 球星

    <!DOCTYPE html>   <html lang="en">   <head>   <meta charset="UTF ...

  8. 使用CMD建立指定格式的文件

    一.建立空文件的几种方法1.cd.>a.txtcd.表示改变当前目录为当前目录,即等于没改变:而且此命令不会有输出.>表示把命令输出写入到文件.后面跟着a.txt,就表示写入到a.txt. ...

  9. Centos6.6 安装Memcached

    一.环境介绍 1)Centos6.4 2) memcached-1.4.24 二.部署安装 计划具体部署步骤: 步骤1:安装 步骤2:配置 步骤3:运行 步骤4:检查 现在开始: 1)安装 $ yum ...

  10. SAP computer之input and MAR

    Input and MAR Below the program counter is the input and MAR block. It includes the address and data ...