Codeforces Round #200 (Div. 2)344D Alternating Current(栈)
来源:互联网 发布:复杂网络理论百度百科 编辑:程序博客网 时间:2024/06/07 14:45
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 movingthe 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.
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.
Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled.
-++-
Yes
+-
No
++
Yes
-
No
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 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 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 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 the device itself:
今天意识到,是不是div2的ABCD只要读懂题就能做出来= =
两根无限长的电线,给你一个 + - 的字符串表示哪个在上面,从第一个交叉开始分电线,如果能解开则输出Yes。
同级出栈,不同级入栈。
AC代码:
#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"using namespace std;const int MAXN = 1e5 + 5;char s[MAXN], str[MAXN];int main(int argc, char const *argv[]){scanf("%s", str);int top = 0, len = strlen(str);for(int i = 0; i < len; ++i)if(top == 0 || s[top] != str[i]) s[++top] = str[i];else --top;if(top == 0) printf("Yes\n"); else printf("No\n");return 0;}
- Codeforces Round #200 (Div. 2)344D Alternating Current(栈)
- Codeforces Round #200 (Div. 2)D. Alternating Current
- Codeforces Round #200 (Div. 2)D. Alternating Current (堆栈)
- Codeforces 344D - Alternating Current
- CodeForces - 344D Alternating Current (模拟题)
- Codeforces 344D Alternating Current【模拟】
- CF#200 div2 D Alternating Current(stack)
- codeforces 343B Alternating Current 【栈】
- Codeforces Round #344 (Div. 2) D. Messenger
- Codeforces Round #344 (Div. 2) D. Messenger
- Codeforces Round #305 (Div. 2) D 栈
- CodeForces 343B Alternating Current
- CodeForces 343B - Alternating Current
- B. Alternating Current----栈
- hdu 1711 && Codeforces Round #344 (Div. 2) D 【KMP】
- Codeforces Round #344 (Div. 2) D. Messenger(kmp)
- Codeforces Round #344 (Div. 2)D. Messenger【kmp】
- Codeforces Round #344 (Div. 2) D. Messenger(kmp,细节)
- cocos studio场景和图层的区别
- cocos2dx源码之 CREATE_FUNC宏
- Monkey测试log分析一
- 上传图片
- activemq配置详解
- Codeforces Round #200 (Div. 2)344D Alternating Current(栈)
- Climbing Stairs
- file样式化
- 浅谈xunsearch、mysql使用及错误总结
- C++集合操作之集合并集:std::set_union
- 网络编程复习_Exp1
- Unity3D之协程(Coroutines & Yield )
- 【傻傻分不清楚】 一 Get和Post
- mysql 新建用户并赋予权限