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
-++-
Yes
+-
No
++
Yes
-
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 简单使用栈的更多相关文章
- CodeForces - 344D Alternating Current (模拟题)
id=46667" style="color:blue; text-decoration:none">CodeForces - 344D id=46667" ...
- [CodeForces 344D Alternating Current]栈
题意:两根导线绕在一起,问能不能拉成两条平行线,只能向两端拉不能绕 思路:从左至右,对+-号分别进行配对,遇到连续的两个“+”或连续的两个“-”即可消掉,最后如果全部能消掉则能拉成平行线.拿两根线绕一 ...
- Codeforces Round #200 (Div. 1) B. Alternating Current 栈
B. Alternating Current Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...
- 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 ...
- C语言 简单的栈
//简单的栈 #include<stdio.h> #include<stdlib.h> //栈的介绍:栈先进后出,一般用于将数据逆序输出 //栈一般只有四种方法--进栈,出栈, ...
- C++编程练习(4)----“实现简单的栈的链式存储结构“
如果栈的使用过程中元素数目变化不可预测,有时很小,有时很大,则最好使用链栈:反之,如果它的变化在可控范围内,使用顺序栈会好一些. 简单的栈的链式存储结构代码如下: /*LinkStack.h*/ #i ...
- C++编程练习(3)----“实现简单的栈的顺序存储结构“
栈(stack)是限定仅在表尾进行插入和删除操作的线性表. 允许插入和删除的一端称为栈顶(top),另一端称为栈底(bottom). 栈又称为后进先出(Last In First Out)的线性表,简 ...
- codeforces 963A Alternating Sum
codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...
- hdu-1237简单计算器(栈的运用)
http://acm.hdu.edu.cn/showproblem.php?pid=1237 简单的栈的运用. 首先将数字和运算符分离,分别保存在两个数组中,然后按原来的式子的顺序,首先将第一个数和第 ...
随机推荐
- linux下安装jdk跟tomcat
文章参考 https://www.cnblogs.com/geekdc/p/5607100.html Linux服务器安装jdk+tomcat https://baijiahao.baidu ...
- Laravel核心解读--ENV的加载和读取
Laravel在启动时会加载项目中的.env文件.对于应用程序运行的环境来说,不同的环境有不同的配置通常是很有用的. 例如,你可能希望在本地使用测试的Mysql数据库而在上线后希望项目能够自动切换到生 ...
- Vue组件通信之Bus
关于组件通信我相信小伙伴们肯定也都很熟悉,就不多说了,对组件通信还不熟悉的小伙伴移步这里. 在vue2.0中 $dispatch 和 $broadcast 已经被弃用.官方文档中给出的解释是: 因为基 ...
- 【codeforces 404D】Minesweeper 1D
[题目链接]:http://codeforces.com/problemset/problem/404/D [题意] 让你玩一个1维的扫雷游戏; 游戏的描述由数字0..2以及符号*表示; 分别表示这个 ...
- 【转】 C#学习笔记14——Trace、Debug和TraceSource的使用以及日志设计
[转] C#学习笔记14——Trace.Debug和TraceSource的使用以及日志设计 Trace.Debug和TraceSource的使用以及日志设计 .NET Framework 命名空 ...
- 黑马程序猿-----Java之你不得不知道的排序
------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS ...
- Linux怎样改动root用户的password
Linux系统的root账号是很重要的一个账号.也是权限最大的一个账号,可是有时候忘了rootpassword怎么办?总不能重装系统吧,这个是下下策.事实上Linux系统中,假设忘记了root账号pa ...
- MFC中CFileDialog使用方法
用CFileDialog选择了一个文件后,使用FILE::fopen打开文件错误,使用 的是相对地址.和王工调试了半天,怎么跟踪也没发现错误,原来如此. .... .. . . CFileDialog ...
- ubuntu16.04下snort的安装(官方文档安装)(图文详解)
不多说,直接上干货! 最近为了科研,需要安装和使用Snort. snort的官网 https://www.snort.org/ Snort作为一款优秀的开源主机入侵检测系统,在windows和Linu ...
- java9新特性-10-语法改进:UnderScore(下划线)使用的限制
1.使用说明 在java 8 中,标识符可以独立使用“_”来命名: 但是,在java 9 中规定“_”不再可以单独命名标识符了,如果使用,会报错: