Description
Mad scientist Mike have just finished constructing a new device to search for extraterrestrial intelligence! He is in such a hurry to launch it's the first time that he plugged in the power wires without giving it a proper glanc E and started experimenting right away. After a while Mike observed, the wires ended up entangled and now has to be untangled again.
The device is powered by and wires "plus" and "minus". The wires run along the floor is from the wall (the "on" to the device). Both The wall and the device have both contacts in them on the same level, into which the wires is plugged in some order. The wires is considered entangled if there is one or more places where one wire runs above the other one. For example, the picture below have four such places (top view):
Mike knows the sequence in which and the wires run above each other. Mike also noticed that on the left side, the ' plus ' wire was always plugged to 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. A wire can is freely moved and stretched on the floor, but cannot is cut.
To understand the problem better, 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 "-" otherw Ise.
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
Input
-++-
Output
Yes
Input
+-
Output
No
Input
++
Output
Yes
Input
-
Output
No
Hint
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 ' crosses in the ', and then Draw it under the "minus" wire, eliminating also the remaining.
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 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:
Test instructions: There are two wires entangled with each other, that is, a wire around a wire, both ends fixed, now want to let you untie them ——— let them parallel to each other not entangled together, condition is These two to the wire can be moved at random on the plane, can also pick up and put down but can not be fixed on both ends of the swap position, asked you can achieve this goal? Analysis: Simulates a simple question. At first, we will find that only the two adjacent nodes (intersections) can be solved simultaneously, and then open the idea of mentally retarded, and want to separate the whole string from the middle, symmetrical Check whether the two sides are the same, and then WA6, think that if the length of the string is odd, the above is not established, wrote a bunch, GG (spicy chicken), and then went online to see the hint, said with the stack in turn, and the stack top elements of the same element to delete the top of the stack, not at the same time, the Then you find the mentally handicapped player +1 .... The first mental retardation code:
1 /*************************************************************************2 > File Name:cfd.cpp3 > Author:4 > Mail:5 > Created time:2016 July 10 Sunday 22:18 49 seconds6 ************************************************************************/7 8#include <iostream>9#include <bits/stdc++.h>Ten using namespacestd; One Const intMAXN = 1e5 +7; A CharSTR[MAXN]; - intMain () - { thescanf"%s", str+1); - intLen = strlen (str+1); - //printf ("len =%d\n", len); - intpos = len/2; + //cout << "pos =" << pos << Endl; - BOOLFlag =true; + for(inti = pos; I >=1; i--) A { at if(Str[i]! = str[len+1-i]) - { -Flag =false; - Break; - } - } in if(!flag | | len = =1) printf ("no\n"); - Else toprintf"yes\n"); + return 0; -}
View Code
Correct code:
1 /*************************************************************************2 > File Name:cfd.cpp3 > Author:4 > Mail:5 > Created time:2016 July 10 Sunday 22:18 49 seconds6 ************************************************************************/7 8#include <iostream>9#include <bits/stdc++.h>Ten using namespacestd; One Const intMAXN = 1e6 +7; Astack<Char>s; - CharSTR[MAXN]; - intMain () the { -scanf"%s", str); - intLen =strlen (str); - for(inti =0; i < Len; i++) + { - if(!s.empty () && str[i] = =s.top ()) + { A S.pop (); at } - Else - { - S.push (Str[i]); - } - } in if(S.empty ()) - { toprintf"yes\n"); + } - Else theprintf"no\n"); * return 0; $}
View Code
Codeforces 344D alternating current simple use stack