Codeforces 344D Alternating Current 简单使用栈

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.。。。刚开始的智障代码:

 1 /*************************************************************************
 2     > File Name: cfd.cpp
 3     > Author:
 4     > Mail:
 5     > Created Time: 2016年07月10日 星期日 22时18分49秒
 6  ************************************************************************/
 7
 8 #include<iostream>
 9 #include<bits/stdc++.h>
10 using namespace std;
11 const int maxn = 1e5 + 7;
12 char str[maxn];
13 int main()
14 {
15     scanf("%s",str+1);
16     int len = strlen(str+1);
17     //printf("len = %d\n",len);
18     int pos = len / 2;
19    // cout << "pos = " << pos << endl;
20     bool flag = true;
21     for(int i = pos; i >= 1; i--)
22     {
23         if(str[i] != str[len+1-i])
24         {
25             flag = false;
26             break;
27         }
28     }
29     if(!flag || len == 1) printf("No\n");
30     else
31        printf("Yes\n");
32     return 0;
33 }

正确代码:

 1 /*************************************************************************
 2     > File Name: cfd.cpp
 3     > Author:
 4     > Mail:
 5     > Created Time: 2016年07月10日 星期日 22时18分49秒
 6  ************************************************************************/
 7
 8 #include<iostream>
 9 #include<bits/stdc++.h>
10 using namespace std;
11 const int maxn = 1e6 + 7;
12 stack<char> s;
13 char str[maxn];
14 int main()
15 {
16     scanf("%s",str);
17     int len = strlen(str);
18     for(int i = 0; i < len; i++)
19     {
20         if(!s.empty() && str[i] == s.top())
21         {
22             s.pop();
23         }
24         else
25         {
26             s.push(str[i]);
27         }
28     }
29     if(s.empty())
30     {
31         printf("Yes\n");
32     }
33     else
34     printf("No\n");
35     return 0;
36 }

时间: 2024-11-05 14:57:17

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

CodeForces - 344D Alternating Current (模拟题)

CodeForces - 344D Alternating Current Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! H

[CodeForces 344D Alternating Current]栈

题意:两根导线绕在一起,问能不能拉成两条平行线,只能向两端拉不能绕 思路:从左至右,对+-号分别进行配对,遇到连续的两个“+”或连续的两个“-”即可消掉,最后如果全部能消掉则能拉成平行线.拿两根线绕一下就理解了,也可以一根拉成直线,另一根围着它绕,然后观察能拉成直线的条件.用栈实现就行. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

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

http://acm.hdu.edu.cn/showproblem.php?pid=1237 简单的栈的运用. 首先将数字和运算符分离,分别保存在两个数组中,然后按原来的式子的顺序,首先将第一个数和第一个运算符分别压 如个自的栈,然后判取出两个栈头部的元素,判断符号,如果是乘除就用当前数值乘取出的数字(优先),然后将乘后的数压入栈, 如果是加则将数和取出的数按原序入栈,如果减,就把新的数变负,将数和取出的数按原序入栈. 最后栈中所有元素的和就是结果. 1 #include<stdio.h> 2

PHP实现简单顺序栈

栈与数组:php数组也有压入压出的方法array_push 和array_shift.是不是数组就是栈数据结构?或者明明数组就可完成,为何还要栈 解答:去全国各地都可以用双腿走到,为何选择火车飞机?数组就是腿,栈就是火车飞机.首先不相等,其次栈可以让我们集中精力去解决更加核心的业务需求. 简单顺序栈实现 class Data{    private $data;     public function __construct($data){         $this->data=$data; 

CodeForces 30C Shooting Gallery 简单dp

题目链接:点击打开链接 给定n个气球 下面n行 x y t val 表示气球出现的坐标(x,y) 出现的时刻t,气球的价值val 枪每秒移动1个单位的距离 问: 射击的最大价值,开始时枪瞄准的位置任意. 思路: dp一下.. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <math.h> #include <set

简单的栈

栈是一种常见的数据结构,主要特点是"后进先出".以下是用C语言实现的简单的栈. 头文件 stack.h ,定义栈的结构体和相关的操作: #ifndef STACK_H #define STACK_H enum { STACK_OK = 0, STACK_OVERFLOW, STACK_ERROR, }; typedef int ElemType; struct stack { ElemType *data; ElemType *top; int capability; }; void 

Codeforces 15C Industrial Nim 简单博弈

题目链接:点击打开链接 题意: 给定n 下面n行,每行2个数u v 表示有v堆石子:u,u+1,u+2···u+v-1 问先手必胜还是后手必胜 思路: 首先根据Nim的博弈结论 把所有数都异或一下,看结果是0还是非0 而这里因为数字太多所以想优化 那么其实对于一个序列 u, u+1, u+2 ···· 显然 {4,5} {,6,7}, {8,9} 这样2个一组的异或结果就是1 那么只需要把序列分组,分成{偶数,奇数} 然后Y一下.. #include<stdio.h> #include<

简单的栈和队列

1 /* 2 入门之栈(Stack)和队列(Queue) 3 在C++中STL中预置了<stack>和<queue> 4 简单介绍栈和队列的思想和使用方法 5 栈:先入后出(LIFO),可以理解为将球放进一个一段封闭的管子,只能从入口区出,先进的球只能最后出来 6 队列:先入先出(FIFO),可以理解为将球放进不封闭的管子,球从另一端出来,先进的球先出 7 常见应用:栈可以用于深搜(DFS),队列可以用于宽搜(BFS) 8 只有看看例子就可以很好的理解栈和队列了,简单的先介绍这些

Codeforces 516D Drazil and Morning Exercise (栈、二分)

题目链接 https://codeforces.com/contest/516/problem/D 题解 我还是数据结构水平太低了啊--连一个点子树内距离不超过\(l\)的点数都不会求 首先有一个熟知的结论是,我们任取原树的一条直径,那么对于任何一个点,直径的两端点中至少有一个到它的距离等于它到所有点的最远距离. 假设直径是\((u_d,v_d)\), 那么我们就把\(u\)的最远距离的式子化简成了\(f_u=\max(dis(u,u_d),dis(u,v_d)\). 考虑\(u_d\)和\(v