Alternating Current
2016-02-24 17:37
387 查看
B. 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
movingthe 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
input
output
input
output
input
output
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:
这个题目拿过来的时候直接懵了,完全没有头绪,唉,还是英语学的烂额,,,大神们拿过题目来就知道啥意思,后来问的解出来的人,才知道这个题目是个啥意思。
两根电线分为正负,在上面的标示出来,不能剪断,想出办法求出这两根电线是否能解开。用的手工创建的栈做的,如果前后两个符号相同,就说明这段内电线没有被缠住,把它从队列当中弹出来就行,碰到不相同的放进去。最后判断这个队列是否为空。
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
movingthe 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:
这个题目拿过来的时候直接懵了,完全没有头绪,唉,还是英语学的烂额,,,大神们拿过题目来就知道啥意思,后来问的解出来的人,才知道这个题目是个啥意思。
两根电线分为正负,在上面的标示出来,不能剪断,想出办法求出这两根电线是否能解开。用的手工创建的栈做的,如果前后两个符号相同,就说明这段内电线没有被缠住,把它从队列当中弹出来就行,碰到不相同的放进去。最后判断这个队列是否为空。
#include <cstdio> #include <iostream> #include <stack> #include <queue> #include <algorithm> #include <cstring> using namespace std; int main() { char x[200000]; while(scanf("%s",x)!=EOF) { int Count=strlen(x); stack<char>p; for (int number1=0;number1<Count;number1++) { if (p.empty()) { p.push(x[number1]); } else if (p.top()==x[number1]) { p.pop(); } else if (p.top()!=x[number1]) { p.push(x[number1]); } } if (p.empty()) { printf("Yes\n"); } else { printf("No\n"); } } return 0; }
相关文章推荐
- Android为什么选择binder
- Android 6.0 fork Zygote时的存储权限管理
- 基于Struts2的供求信息网设计(三)
- 用Android代码实现打开USB调试
- Tomcat Connector三种运行模式(BIO, NIO, APR)的比较和优化
- use-gulp
- 别人java的博客。
- 【codevs1287】矩阵乘法(矩阵乘法)
- 利用postmessage间接实现iframe跨域调用父页面js函数
- pch 文件的创建
- View的事件分发机制记录
- horizon翻译
- Java相对路径读取文件
- android学习网站
- Oracle sql语句创建表空间、数据库、用户及授权
- PAT-1016 部分A+B
- 【特征匹配】BRISK原文翻译
- 网站就必须用响应式布局吗?MVC视图展现模式之移动布局
- 常用正则表达式
- 对 clear:both 这个样式的一些理解