CodeForces 343B - Alternating Current
2015-07-29 00:24
381 查看
原文(有图):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:
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"); else printf("No\n"); } return 0; }
相关文章推荐
- 分组背包 hdu3033 I love sneakers!
- STM32CubeF4 FreeRTOS Examples don't work correctly with HAL_GetTick
- iOS开发之软键盘使用小技巧
- MQ、JMS以及ActiveMQ 关系的理解
- 控件布局——LinearLayout(下)
- 我解决了oom
- CentOS 7 下使用 Firewall
- 第一篇:AutoCAD实体类
- 杭电4006 The kth great number
- 解决MyEclipse添加自定义库后,提示class not found
- 说的太经典了,太现实了。顶!
- 写给还在迷惘中的大学生(计算机学院)
- 2015/07/28
- Eclipse下maven部署web项目到tomcat7(兼容tomcat8)
- CodeForces 270B - Multithreading
- cstring,string,string.h 区别比较
- CodeIgniter 用户指南 基础教程 摘要
- hdu 5225 Tom and permutation(回溯)
- Java多态学习总结
- 黑马程序员——Java基础---泛型、集合框架工具类:Collections和Arrays、JDK 1.5新特性