Codeforces Round #200 (Div. 2)D. Alternating Current (堆栈)
1 second
256 megabytes
standard input
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.
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.
Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled.
-++-
Yes
+-
No
++
Yes
-
No
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 (堆栈)的更多相关文章
- 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. 1 + Div. 2)
A. Magnets 模拟. B. Simple Molecules 设12.13.23边的条数,列出三个等式,解即可. C. Rational Resistance 题目每次扩展的电阻之一是1Ω的, ...
- 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 ...
- Codeforces Round #200 (Div. 1)A. Rational Resistance 数学
A. Rational Resistance Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...
- 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/ ...
- 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 ...
- Codeforces Round #200 (Div. 1) BCD
为了锻炼个人能力奋力div1 为了不做原题从200开始 B 两个电线缠在一起了 能不能抓住两头一扯就给扯分开 很明显当len为odd的时候无解 当len为偶数的时候 可以任选一段长度为even的相同字 ...
- Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)
思路: dfs序其实是很水的东西. 和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...
- Codeforces Round #200 (Div. 2) E. Read Time(二分)
题目链接 这题,关键不是二分,而是如果在t的时间内,将n个头,刷完这m个磁盘. 看了一下题解,完全不知怎么弄.用一个指针从pre,枚举m,讨论一下.只需考虑,每一个磁盘是从右边的头,刷过来的(左边来的 ...
随机推荐
- Spring Data 自动生成
之前一直用的mybatis逆向自动生成,由于最近学习springdata,所以看了一下springdata的自动生成,基本与mybatis一致,不同的也许就是逆向生成代码(实体类,mapper等)和正 ...
- 【寒假集训系列DAY3】
DAY2的坑之后会补上 DAY3 今天暴力分拿的还是不错的...60+30+25=115,但还是太弱了呀,每题都只会暴力(话说第3题直接输-1给了15分,本以为只会给5分,然后打了半个小时的爆搜... ...
- 利用windbg获取dump的dll文件
根据堆栈对应的地址查找其对应的Module ID,然后将对应的Module保存. !IP2MD 命令从托管函数中获取 MethodDesc 结构地址. !dumpmodule 1caa50 下面的命令 ...
- Oracle update时做表关联
感觉还是sqlserver中的写法比较好理解,Oracle的写法都快把我搞晕了, 注意: 1.要修改的表,不要加入到子查询中,用别名在子查询中与其他表进行关联即可. 2.exsits不能少,exsit ...
- 打开手机摄像头扫描二维码或条形码全部操作(代码写的不好,请提出指教,共同进步,我只是一个Android的小白)
(1)下载二维码的库源码 链接:http://pan.baidu.com/s/1pKQyw2n 密码:r5bv 下载完成后打开可以看到 libzxing 的文件夹,最后添加进 Android Stu ...
- ★Java-----记事本编译、运行时注意事项
1.文件名需要与源代码中公共类的名字相同,即class后面的名字: 2.Java中严格区分大小写: 3.记事本编辑好之后保存文件后缀必须是". java": 4.运行cmd,dos ...
- 使用 C# 进行 HTTP 操作
说明 主要使用到了 Newtonsoft.Json 和 System.Net 两个命名空间. Program.cs static void Main(string[] args) { WebOpert ...
- 【sqli-labs】 less16 POST - Blind- Boolian/Time Based - Double quotes (基于bool型/时间延迟的双引号POST型盲注)
' or 1=1# -->失败 1" or 1=1# -->失败 1') or 1=1# -->失败 1") or 1=1# -->成功 判断为双引号变形注 ...
- python自动发邮件库yagmail(转)
一般发邮件方法 我以前在通过Python实现自动化邮件功能的时候是这样的: import smtplib from email.mime.text import MIMEText from email ...
- BZOJ 1108: [POI2007]天然气管道Gaz 性质分析_小结论_巧妙
Description Mary试图控制成都的天然气市场.专家已经标示出了最好的天然气井和中转站在成都的地图.现在需要将中转 站和天然气井连接起来.每个中转站必须被连接到正好一个钻油井,反之亦然. M ...