CodeForces 343B - Alternating Current

来源:互联网 发布:淘宝森系口碑好的店铺 编辑:程序博客网 时间:2024/05/29 08:46

原文(有图):http://codeforces.com/problemset/problem/343/B

Mad scientist Mike has just finished constructing a new device to searchfor extraterrestrial intelligence! He was in such a hurry to launch it for thefirst time that he plugged in the power wires without giving it a proper glanceand started experimenting right away. After a while Mike observed that thewires 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) tothe device (on the right). Both the wall and the device have two contacts inthem on the same level, into which the wires are plugged in some order. Thewires are considered entangled if there are one or more places where one wireruns 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. Mikealso noticed that on the left side, the "plus" wire is always pluggedinto the top contact (as seen on the picture). He would like to untangle thewires without unplugging them andwithout moving thedevice. Determine if it is possible to do that. A wire can be freely moved andstretched on the floor, but cannot be cut.

To understand the problem better please read the notes to the testsamples.

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. Tountangle the wires, one can first move the "plus" wire lower, thuseliminating 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 fullrevolution around the "minus" wire. Thus the wires cannot beuntangled:

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 thedevice itself:

 

程序:#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <stack>using namespace std;#define L(u) (u<<1)#define R(u) (u<<1|1)#define lowbit(x) (x&-x)#define rep(i,x,y) for (i=x;i<=y;i++)#define ll __int64#define max(x,y) ((x>y)?(x),(y))#define min(x,y) ((x<y)?(x),(y))#define sd(x) scanf("%d",&x)#define sd2(x,y) scanf("%d%d",&x,&y)#define slld(x) scanf("%lld",&x)const int N = 100005;struct node{int x, y;};bool cmp(node a, node b){return a.x<b.x;}char st[100005];int main(){stack<char> q;int i;scanf("%s", st);string st2 = "";int len = strlen(st);if (len % 2 == 1){printf("No\n");}else{for (i = 0; i<len; i++){q.push(st[i]);if (q.size()>1){while (q.size()>1){char t1, t2;t1 = q.top();q.pop();t2 = q.top();q.pop();if (t1 != t2){q.push(t2);q.push(t1);break;}}}}if (q.empty())printf("Yes\n");elseprintf("No\n");}return 0;}

0 0
原创粉丝点击