B. Alternating Current----栈
来源:互联网 发布:linux route add 网关 编辑:程序博客网 时间:2024/06/08 02:46
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:
题目链接:http://codeforces.com/contest/343/problem/B
这个题卡题意了。。。看了半天没看懂。。。无奈求助了谷歌翻译,然后这个题很简单,,,为什么会出现在div1里。。
题意是说我们通过在平面上移动红蓝两条线能否让这两条线分开,+代表交叉处红在上,-代表交叉处蓝在上。
首先我们很容易就知道奇数个肯定不行,pass掉,然后我们看提示中画的第二个图,很容易就可以看出来如果要是想要消掉的话我们需要满足两个条件,一个是连续,另一个是相同,所以我们维护一个简单栈就可以了。。。
代码:
#include <cstdio>#include <cstring>#include <iostream>using namespace std;char s[200000];int xin[200000];int main(){ int top=0; scanf("%s",s); int len=strlen(s); if(len%2){ printf("No\n"); return 0; } int k; for(int i=0;i<len;i++){ if(s[i]=='+'){ k=1; } else{ k=-1; } if(top!=0&&xin[top-1]==k){ top--; } else{ xin[top++]=k; } } if(top==0){ printf("Yes\n"); } else{ printf("No\n"); } return 0;}
- B. Alternating Current----栈
- codeforces 343B Alternating Current 【栈】
- CodeForces 343B Alternating Current
- CodeForces 343B - Alternating Current
- Alternating Current
- Alternating Current
- Alternating Current
- Codeforces Round #200 (Div. 2)344D Alternating Current(栈)
- Codeforces 344D - Alternating Current
- L - Alternating Current -链表贪心
- CodeForces - 344D Alternating Current (模拟题)
- CF#200 div2 D Alternating Current(stack)
- Codeforces 344D Alternating Current【模拟】
- Codeforces Round #200 (Div. 2)D. Alternating Current
- Codeforces Round #200 (Div. 2)D. Alternating Current (堆栈)
- Alternating Sums
- fjnu 1281 Alternating Sums
- ZOJ 3813 Alternating Sum
- 使用HAproxy如何实现web站点的动静分离
- Xamarin XAML语言教程控件模板的模板绑定
- 浅谈内存对齐和位域
- C++调用Cimiss接口返回值
- 喷水装置(一)NYOJ--6(贪心)
- B. Alternating Current----栈
- 联想 笔记本 改WIN7 BIOS 设置详解
- Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术
- Java中同一个类中不同的synchronized方法是否可以并发执行 ?
- 一种方便处理负数下标数组的方法
- Linux 下 zip 压缩/解压缩 命令的使用
- nginx 反向代理的基本配置(以Ubuntu 16.04LTS为例)
- 在Docker上使用Weave搭建Hadoop和Spark跨主机容器集群
- go slice坑