Codeforces Round #200 (Div. 1) B. Alternating Current 栈
B. Alternating Current
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
Output
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"); }