Alternating Current
来源:互联网 发布:淘宝新店第一天刷几单 编辑:程序博客网 时间:2024/06/15 04:04
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):
![](http://codeforces.com/predownloaded/76/24/76248fd38be4153cc8315ac64ea915dc67a86af2.png)
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.
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.
Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled.
-++-
Yes
+-
No
++
Yes
-
No
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:
![](http://codeforces.com/predownloaded/81/e1/81e1edb99fea49c156f8be027eb8db015dbc525e.png)
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:
![](http://codeforces.com/predownloaded/9e/a0/9ea037310e720f81e1cbe65cb98b03509b7f3c18.png)
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/predownloaded/77/42/7742c4a5e3b08a724d1c0abef994ca2ec611507a.png)
#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;}
- Alternating Current
- Alternating Current
- Alternating Current
- B. Alternating Current----栈
- CodeForces 343B Alternating Current
- CodeForces 343B - Alternating Current
- Codeforces 344D - Alternating Current
- L - Alternating Current -链表贪心
- CodeForces - 344D Alternating Current (模拟题)
- codeforces 343B Alternating Current 【栈】
- CF#200 div2 D Alternating Current(stack)
- Codeforces 344D Alternating Current【模拟】
- Codeforces Round #200 (Div. 2)D. Alternating Current
- Codeforces Round #200 (Div. 2)344D Alternating Current(栈)
- Codeforces Round #200 (Div. 2)D. Alternating Current (堆栈)
- Alternating Sums
- fjnu 1281 Alternating Sums
- ZOJ 3813 Alternating Sum
- Fragment加viewpager+fragment viewpager中的fragment显示空白
- ViewGroup事件分发处理
- android学习网站
- Android为什么选择binder
- 无法定位程序输入点 于动态链接QtCore4.dll库上
- Alternating Current
- Android 6.0 fork Zygote时的存储权限管理
- 在release(发布app)中屏蔽NSLog输出语句
- C++学习笔记:虚析构函数
- Android4.4的zygote进程
- 【CSS3】matrix
- Xcode中archive打包的时候,出现you are not allowed to perform this operation.please..问题
- 关于微信SDk的方法(1)
- linux下如何查看文件权限