Description

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 Input

Input
-++-
Output
Yes
Input
+-
Output
No
Input
++
Output
Yes
Input
-
Output
No

Hint

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:

题意:有两根导线相互纠缠,也就是说一根导线绕一根导线,两端固定,现在想让你把他们解开———让他们相互平行不在缠绕在一起,条件是这两根到导线可以在平面上随意移动,也可以拿起放下但是不能让两端固定的交换位置,问你是否可以达到这个目标?分析:模拟简单题。。刚开始通过提示会发现只有相邻两个结点(交叉点)相同时这两个导线可以解开,于是开启智障想法,想通过对整个字符串从中间分开,对称的检查两边是否一样来解,然后就WA6,想想发现如果字符串的长度是奇数的话,以上不成立,写了一堆,GG(辣鸡),之后去网上看了提示,说用栈依次处理,和栈顶元素比较相同的元素删除栈顶,不同时进栈,最后统计栈是否为空即可。然后就发现智障选手+1.。。。刚开始的智障代码:

 /*************************************************************************
> File Name: cfd.cpp
> Author:
> Mail:
> Created Time: 2016年07月10日 星期日 22时18分49秒
************************************************************************/ #include<iostream>
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ;
char str[maxn];
int main()
{
scanf("%s",str+);
int len = strlen(str+);
//printf("len = %d\n",len);
int pos = len / ;
// cout << "pos = " << pos << endl;
bool flag = true;
for(int i = pos; i >= ; i--)
{
if(str[i] != str[len+-i])
{
flag = false;
break;
}
}
if(!flag || len == ) printf("No\n");
else
printf("Yes\n");
return ;
}

正确代码:

 /*************************************************************************
> File Name: cfd.cpp
> Author:
> Mail:
> Created Time: 2016年07月10日 星期日 22时18分49秒
************************************************************************/ #include<iostream>
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + ;
stack<char> s;
char str[maxn];
int main()
{
scanf("%s",str);
int len = strlen(str);
for(int i = ; i < len; i++)
{
if(!s.empty() && str[i] == s.top())
{
s.pop();
}
else
{
s.push(str[i]);
}
}
if(s.empty())
{
printf("Yes\n");
}
else
printf("No\n");
return ;
}

Codeforces 344D Alternating Current 简单使用栈的更多相关文章

  1. CodeForces - 344D Alternating Current (模拟题)

    id=46667" style="color:blue; text-decoration:none">CodeForces - 344D id=46667" ...

  2. [CodeForces 344D Alternating Current]栈

    题意:两根导线绕在一起,问能不能拉成两条平行线,只能向两端拉不能绕 思路:从左至右,对+-号分别进行配对,遇到连续的两个“+”或连续的两个“-”即可消掉,最后如果全部能消掉则能拉成平行线.拿两根线绕一 ...

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

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

  4. Codeforces Round #200 (Div. 2)D. Alternating Current (堆栈)

    D. Alternating Current time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. C语言 简单的栈

    //简单的栈 #include<stdio.h> #include<stdlib.h> //栈的介绍:栈先进后出,一般用于将数据逆序输出 //栈一般只有四种方法--进栈,出栈, ...

  6. C++编程练习(4)----“实现简单的栈的链式存储结构“

    如果栈的使用过程中元素数目变化不可预测,有时很小,有时很大,则最好使用链栈:反之,如果它的变化在可控范围内,使用顺序栈会好一些. 简单的栈的链式存储结构代码如下: /*LinkStack.h*/ #i ...

  7. C++编程练习(3)----“实现简单的栈的顺序存储结构“

    栈(stack)是限定仅在表尾进行插入和删除操作的线性表. 允许插入和删除的一端称为栈顶(top),另一端称为栈底(bottom). 栈又称为后进先出(Last In First Out)的线性表,简 ...

  8. codeforces 963A Alternating Sum

    codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...

  9. hdu-1237简单计算器(栈的运用)

    http://acm.hdu.edu.cn/showproblem.php?pid=1237 简单的栈的运用. 首先将数字和运算符分离,分别保存在两个数组中,然后按原来的式子的顺序,首先将第一个数和第 ...

随机推荐

  1. Python+Appium来写app自动化脚本

    1...........................我有空再补

  2. 【codeforces 65A】Harry Potter and Three Spells

    [题目链接]:http://codeforces.com/problemset/problem/65/A [题意] 你有3种魔法; 1.可以将a单位的石头变成b单位的铅 2.可以将c单位的铅变成d单位 ...

  3. C# - Thread.Join()

    Blocks the calling thread until a thread terminates, while continuing to perform standard COM and Se ...

  4. POJ 3173 模拟

    按照题意模拟就好-- //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; in ...

  5. linux中的挂载是什么意思?通俗点讲

    mount /dev/sda1 /mnt解释:mount 就是挂载命令,/dev/sda1是要挂载的磁盘分区,/mnt是要绑定的目录挂载后就能到目录/mnt去访问磁盘分区/dev/sda1里面的资料了 ...

  6. html中隐藏一个元素的方法

    display:none;                                                      隐藏不占位 opacity:0; fliter:alpha(opa ...

  7. 关于概率算法的问题,不知道逻辑错在哪里,求debug

    做个骰子成功几率的分析,投n颗骰子,第一次投成功的几率是a,然后投成功的骰子,需要再投1次,这次成功的几率是b.第二次成功的骰子才算最终成功. 要分析出n颗骰子,最终成功0到n颗的概率. 我写了个算法 ...

  8. PostgreSQL Replication之第九章 与pgpool一起工作(2)

    9.2 理解pgpool的功能 pgpool提供了如下功能: •连接池 •语句级别的复制 •负载均衡 •限制连接 •内存缓存 •并行查询 [当决定使用那些功能的时候,记住并非所有的功能可以在同一时间使 ...

  9. 关于table布局

    html-table 宝贝 状态 单价 数量 商品总价 运费 1sdsdf 2 3fffff 4sdfsfsffsdfs 5dsfs 6

  10. hp服务器安装exsi5.5

    启动按f8进入raid制造页面: 1.  插入exsi5.5光盘 2.  按下开机键(开机比较慢需要等待一段时间) 3.  进入启动项目界面(插入光盘后启动会让你选择启动项.选择1光盘启动) 接下来按 ...