Codeforces Round #200 (Div. 1) B. Alternating Current 栈
2015-10-12 10:56
441 查看
[b]B. Alternating Current[/b]
Time Limit: 1 Sec
Memory Limit: 256 MB
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.
Sample Output
有两条直线缠绕在一起,一条直线是+,一条直线是-
如果+就表示第一条直线在上面,如果是-,就表示第二条直线在上面
问你能否直接拉,就能把这两条直线拉成平行线
题解:
首先我们想一想,必须是偶数个才行,不然的话,根本不可能拉成平行线
必须得两个连在一起的符号一样才能消除,于是我们就用栈来搞定就好啦
代码:
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/343/problem/BDescription
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 Input
-++-
Sample Output
Yes
HINT
题意有两条直线缠绕在一起,一条直线是+,一条直线是-
如果+就表示第一条直线在上面,如果是-,就表示第二条直线在上面
问你能否直接拉,就能把这两条直线拉成平行线
题解:
首先我们想一想,必须是偶数个才行,不然的话,根本不可能拉成平行线
必须得两个连在一起的符号一样才能消除,于是我们就用栈来搞定就好啦
代码:
#include<stdio.h> #include<stack> #include<iostream> using namespace std; string S; int main() { stack<char> s; cin>>S; for(int i=0;i<S.size();i++) { char ch = S[i]; if(!s.empty()&&ch==s.top())s.pop(); else s.push(ch); } if(s.empty())printf("Yes\n"); else printf("No\n"); }
相关文章推荐
- 苹果公司技术支持联系电话
- ShaderLab Tutorials-Alpha Testing
- 第五更 android 自定义view(一)
- 关于怎么绑定Jquery 的scroll事件(兼容浏览器)
- JS实现新浪微博效果带遮罩层的弹出框代码
- 浏览器兼容性总结
- centos7安装nginx+mariadb+php-fpm
- ios 国际化(项目名和内容)
- 74个Swift标准库函数
- 在安卓JNI/NDK中使用C++11
- vs2008打包过程图解
- SSH与MVC
- VIM常用快捷键
- Javer的漫谈
- 移动App崩溃的测试用例设计
- 资源管理——对象池
- lintcode 容易题:Merge Sorted Array II 合并排序数组 II
- linux下安装wine
- scikit-learn
- Android studioAPK获取签名的方法