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


    D. Alternating Current
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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.

    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 test(s)
    Input
    -++-
    
    Output
    Yes
    
    Input
    +-
    
    Output
    No
    
    Input
    ++
    
    Output
    Yes
    
    Input
    -
    
    Output
    No
    
    Note

    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
    ");
    		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 "); else printf("No "); }



  • 相关阅读:
    Chroot 特性 ?
    服务端处理 Watcher 实现 ?
    四种类型的数据节点 Znode ?
    Zookeeper 文件系统 ?
    ZooKeeper 面试题?
    Mapper 编写有哪几种方式?
    Mybatis 的一级、二级缓存?
    Mybatis 是否支持延迟加载?如果支持,它的实现原理是什么?
    一对一、一对多的关联查询 ?
    Mybatis 的 Xml 映射文件中,不同的 Xml 映射文件,id 是否可以重复?
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6915330.html
Copyright © 2020-2023  润新知