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. 线性预测与Levinson-Durbin算法实现

    在学习信号处理的时候,线性预测是一个比较难理解的知识点,为了加快很多朋友的理解,这里给出Levinson-Durbin算法的线性预测实现和一个测试Demo,Demo中很明确的把输入信号.预测信号.预测 ...

  2. oracle-3种工具使用

    1:原命令行,dos 2:sqlplus,图形界面 3:isqlplus,网页版的.(假如自己机器无法安装oracle,可通过别人ip地址去使用oracle,http://ip:5560/isqlpl ...

  3. 从Hive1.0升级到Hive2.0(删除hive1.x中的元数据, 并不是数据迁移)

    软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...

  4. java这个404你能解决吗?

    前言 本文首发于公众号[我的小碗汤]本公众号免费提供csdn下载服务,海量IT学习资源,如果你准备入IT坑,励志成为优秀的程序猿,那么这些资源很适合你,包括但不限于java.go.python.spr ...

  5. uni-app强大的前端框架,h5,原生app(两大系统),微信小程序

    最近发现一款强大的前端框架,它叫uni-app 这是一款通用的框架可以打包app,h5,微信小程序, 说说要弄这个工具需要会那些技能吧, 要熟悉vue,微信小程序.这样这个框架用的就是很快上手了 模块 ...

  6. apicloud 第三方登录授权、微信、扣扣、微博登录授权

    授权登录.接入第三方的配置 例如:微信的登录授权. 首先在模块里面添加 wx 这个模块,然后在项目的配置文件里面进行配置. 配置的时候要现在微信开放平台 https://open.weixin.qq. ...

  7. BZOJ 3319: 黑白树 树+并查集+未调完+神题

    Code: #include<bits/stdc++.h> #define maxn 1000003 using namespace std; char *p1,*p2,buf[10000 ...

  8. Django—链接MySQL

    Djalgo基础配置方法 静态文件配置方法 1 所有的静态文件都放在 static 文件夹下,例如Bootstrap值类的第三方库,通常 static 文件下会创建 css image js 文件,用 ...

  9. LINUX - getopts

    getopts optionString opt; optionString :所有参数组成的-参数串: opt:从optionString 每次取的参数值: 当optionString用[:]开头, ...

  10. javascript实现:在N个字符串中找出最长的公子串

    'use strict' module.exports = function 找出最长公子串 (...strings) { let setsOfSubstrings = [] strings.redu ...