[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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define X                   first
#define Y                   second
#define pb                  push_back
#define mp                  make_pair
#define all(a)              (a).begin(), (a).end()
#define fillchar(a, x)      memset(a, x, sizeof(a))
#define copy(a, b)          memcpy(a, b, sizeof(a))

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

//#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
//#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
template<typename T>
void V2A(T a[],const vector<T>&b){for(int i=0;i<b.size();i++)a[i]=b[i];}
template<typename T>
void A2V(vector<T>&a,const T b[]){for(int i=0;i<a.size();i++)a[i]=b[i];}

const double PI = acos(-1.0);
const int INF = 1e9 + 7;
const double EPS = 1e-8;

/* -------------------------------------------------------------------------------- */

char s[123456];

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
    while (~scanf("%s", s)) {
        stack<char> S;
        for (int i = 0; s[i]; i ++) {
            char ch = s[i];
            if (!S.empty() && ch == S.top()) S.pop();
            else S.push(ch);
        }
        puts(S.empty()? "Yes" : "No");
    }
    return 0;
}
时间: 2024-10-17 04:37:34

[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 简单使用栈

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 st

Codeforces 223A Bracket Sequence [栈]

给一串由(, ), [ ,]构成的字符串,求包含[最多的合法子串 很容易,先把整个字符串丢入栈里处理 栈的每一个元素存两个东西,字符,在字符串中的位置 处理方式为如果是()匹配则直接丢弃,如果是[]匹配则在这个点vis[i]++,然后求vis的前缀和 如果栈空,则说明整个串是合法的,直接输出串 否则,扫描栈中剩下的元素的位置,这几个位置把整个原串切割成几段,这几段肯定是合法的,求这几段中的含[最大的段,可以用前缀和求得,输出这段 游标,左右区间非常容易弄错.有一次手动输出了'\0',导致看起来和

Codeforces 963A Alternating Sum 【数论+数学】

官方题解这个样子我觉得说得比较清楚.Z我们可以朴素的预处理出来(注意乘法膜),q的话考点在于[分数取膜]即 (a/b)%P = a* inverse of b %P 这就涉及到算b的逆元,我用的是欧几里得算法.下面这个博客写的很清楚. http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html 然后还有两个细节,一是要写快速幂这样才能 O(k * log(n)) 复杂度预处理出Z,快速幂的时候注意a的类型得是long lon

CodeForces 344

这次是作为晚训题目做的,代码都挺短,考的是思维,想到了分分钟AC,想不到就依然是想不到-- A - Magnets Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 344A Description Mad scientist Mike entertains himself by arranging rows of domi

Codeforces Round #200 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/344 A. Magnets time limit per test:1 second memory limit per test:256 megabytes Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets inst

跟着xiaoxin巨巨做cf

cf 385 C. Bear and Prime Numbers 题目大意:有一个数列{xi},每次给出一个询问[l, r],即问 S(l ,r)是l和r之间的素数,f(p)表示数列{xi}中整除p的个数 思路:筛法,显然xi的顺序是无所谓的,我们把它记录下来,然后做筛法,如果一个素数筛合数的过程中遇到一个数的话显然得记到这个素数的头上,然后再把这样得到的表求一个sum就行 1 #include<iostream> 2 #include<cstdio> 3 #define maxn

托福分类词汇

托福分类词汇表(共17类863个常用单词) 一.   psychology 心理 1.       mental 心理的 2.       physical 身体的,物质的,物理的 3.       spiritual 心灵的 4.       conformity 从众 5.       majority 多数人 6.       minority 少数人 7.       threshold judgment (心理学)初始性判断 8.       subject 受实验对象 9.     

ieee trans on energy conversion 2

Z. Wang, W. Wu and B. Zhang, "A Distributed Control Method With Minimum Generation Cost for DC Microgrids," in IEEE Transactions on Energy Conversion, vol. 31, no. 4, pp. 1462-1470, Dec. 2016.doi: 10.1109/TEC.2016.2584649 keywords: {distributed