D. Alternating Current
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 moving the 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.

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. 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/problemset/problem/344/D

题目大意:有两根导线(+线和-线)连接着电源和移动设备,+表示+线在 - 线上面。- 表示 - 线在+线上面。它们缠绕在一起,求能否在不移动电源和设备的条件下把两根线分开。

解题思路:栈模拟。一開始用搜索写超时了,后来才知道是一题脑洞题,堆栈模拟就能做出来。观察发现导线相交点为奇数个时一定不能分开。由于终于会有至少一个点相交,而要分开的条件是相交点两两相应相等,用栈实现就是:若当前点与栈顶元素相等,则它们是相应的,抛出。反之,不是相应的,压入。等待推断下一个。

代码例如以下:

#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
const int maxn=1000005;
char s[maxn];
int n;
int main(void)
{
scanf("%s",s);
n=strlen(s);
if(n%2)
{
printf("No\n");
return 0;
}
stack<char>st;
for(int i=0;i<n;i++)
{
if(!st.size()) //假设栈里没有元素,压入字符
st.push(s[i]);
else
{
char t=st.top();
if(t==s[i]) //相等,则它们是相应的,此处导线可分开抛出。 st.pop();
else //不是相应的,压入,等待推断下一个。
st.push(s[i]);
}
}
if(!st.size())
printf("Yes\n");
else
printf("No\n");
}

Codeforces Round #200 (Div. 2)D. Alternating Current (堆栈)的更多相关文章

  1. Codeforces Round #200 (Div. 1) B. Alternating Current 栈

    B. Alternating Current Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...

  2. Codeforces Round #200 (Div. 1 + Div. 2)

    A. Magnets 模拟. B. Simple Molecules 设12.13.23边的条数,列出三个等式,解即可. C. Rational Resistance 题目每次扩展的电阻之一是1Ω的, ...

  3. Codeforces Round #200 (Div. 1) C. Read Time 二分

    C. Read Time Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/C ...

  4. Codeforces Round #200 (Div. 1)A. Rational Resistance 数学

    A. Rational Resistance Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...

  5. Codeforces Round #200 (Div. 1)D. Water Tree dfs序

    D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...

  6. Codeforces Round #200 (Div. 2) C. Rational Resistance

    C. Rational Resistance time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. Codeforces Round #200 (Div. 1) BCD

    为了锻炼个人能力奋力div1 为了不做原题从200开始 B 两个电线缠在一起了 能不能抓住两头一扯就给扯分开 很明显当len为odd的时候无解 当len为偶数的时候 可以任选一段长度为even的相同字 ...

  8. Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)

    思路: dfs序其实是很水的东西.  和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...

  9. Codeforces Round #200 (Div. 2) E. Read Time(二分)

    题目链接 这题,关键不是二分,而是如果在t的时间内,将n个头,刷完这m个磁盘. 看了一下题解,完全不知怎么弄.用一个指针从pre,枚举m,讨论一下.只需考虑,每一个磁盘是从右边的头,刷过来的(左边来的 ...

随机推荐

  1. gitlab克隆报错:remote: HTTP Basic: Access denied;remote: You must use a personal access token with ‘api’ scope for Git over HTTP.

    错误: remote: HTTP Basic: Access denied remote: You must use a personal access token with ‘api’ scope ...

  2. POJ 1470 Tarjan算法

    裸的LCA,读入小坑.Tarjan算法大坑,一开始不知道哪儿错了,后来才发现,是vis数组忘了清零了(⊙﹏⊙)b 傻傻的用了邻接矩阵...很慢啊,1100多ms. Closest Common Anc ...

  3. Python yield解析

    Pyhton generators and the yield keyword At a glance,the yield statement is used to define generators ...

  4. python--7、面向对象

    什么是面向对象 对象,即抽象的一类事物中的某个具体的个体.这个世界中存在的一切皆为对象,不存在的也能创建出来. 较之面向过程的区别: 编程的复杂度远高于面向过程,不了解面向对象而立即上手基于它设计程序 ...

  5. Shiny学习实践01

    Shiny是什么东东? 官方描述: Shiny is an R package that makes it easy to build interactive web apps straight fr ...

  6. 用js制作一个计算器

    使用js制作计算器 <!doctype html> <html lang="en"> <head> <meta charset=" ...

  7. (转) OpenLayers3基础教程——OL3 介绍control

    http://blog.csdn.net/gisshixisheng/article/details/46761535 概述: 本文讲述的是Ol3中的control的介绍和应用. OL2和OL3 co ...

  8. crypto-js RC4和hash_hmac运用

    遇到一个问题,前端需要加密,可能用到一些算法,推荐这个库:crypto-js, RC4是一个可逆的加密,看下用法: import CryptoJS from 'crypto-js'; const RC ...

  9. Day 20 python基础总复习

    一.计算机基础 1.1 计算机基础之编程 编程语言是人与计算机之间交流的介质 编程就是写一堆文件 编程为了奴隶计算机,解放劳动力 1.2 计算机组成原理 CPU 控制器:控制硬件 运算器:逻辑运算和算 ...

  10. python 生成HTmL报告页面 V1.3 修改字体颜色

    HTML报告V1.3 根据文字内容显示不同的字体颜色: 代码如下: # -*- coding=utf-8 -*- import time,os """ V1.2 1.生成 ...