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 简单的栈的运用. 首先将数字和运算符分离,分别保存在两个数组中,然后按原来的式子的顺序,首先将第一个数和第 ...
随机推荐
- enterprise architect (EA) 源码生成UML类图,帮助理解项目工程
用VS看大型工程代码,尤其是很多层类的,很容易头晕,即便是装了visual assist 插件.用VS生成类图吧,只能生成一堆框,只有一些小的类关系有箭头表示.远远不能满足要求.下面介绍建模工具EA来 ...
- 【Linux编程】socket编程
套接字是通信端点的抽象.文件描写叙述符用open函数创建,而套接字描写叙述符用socket函数创建.socket函数原型例如以下: int socket(int domain, int type, i ...
- TCP/IP、UDP、 Http、Socket的差别
网络由上往下分为: 表示层和应用层 :HTTP协议(基于传输层的TCP协议,主要解决怎样包装数据) 会话层 传输层: TCP协议(基于网络层的IP协议).TPC/IP协议(主要解决数据怎样在网络中传输 ...
- Codeforces Round #256 (Div. 2) B. Suffix Structures(模拟)
题目链接:http://codeforces.com/contest/448/problem/B --------------------------------------------------- ...
- 腾讯之困,QQ与微信各有各的烦恼
QQ渐渐在腾讯内部弱化 在PC时代,QQ是即时通讯领域当之无愧的王者.但在微信崛起后,手机QQ未来会被微信替代的判断喧嚣至上. 早在2012年就有传言腾讯在游戏领域開始去"娱乐化" ...
- How to: Create Custom Configuration Sections Using ConfigurationSection
https://msdn.microsoft.com/en-us/library/2tw134k3.aspx You can extend ASP.NET configuration settings ...
- 使用OpenCV把二进制mnist数据集转换为图片
mnist数据集是以二进制形式保存的,这里借助OpenCV把mnist数据集转换成图片格式.转换程序如下: #include <iostream> #include <fstream ...
- rest_framework 认证功能
from django.views import View from rest_framework.views import APIView from rest_framework.authentic ...
- 基于jquery 的find()函数和children()函数的区别
element.find(selector) 返回匹配element集合中每个元素的后代,参数selector是必须的,可以通过选择器对元素进行过滤,筛选出符合条件的元素.如果想选中所有的后代元素, ...
- vuejs helloworld
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...