Alternating Current

Codeforces Round #200 (Div. 1) B:http://codeforces.com/problemset/problem/343/B

题意:这一题看懂题意是关键,题目的意思就是两根a,b电线相互缠绕,+表示a在b的上面,-表示b在a的上面,给以一个串,然后问你这两根线能否解开。

题解:显然,如果两个相邻的++或者--的话可以直接消去,然后就会想到用栈来模拟,相同的就消去,不同的就进栈,最后判断栈内是否为空就可以了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int N=1e5+10;
 7 char ans[N],a[N];
 8 int n,top;
 9 int main(){
10    while(~scanf("%s",a)){
11       n=strlen(a);top=0;
12       for(int i=0;i<n;i++){
13         if(top==0){
14             ans[++top]=a[i];
15         }
16         else if(a[i]==ans[top]){
17             top--;
18         }
19         else  ans[++top]=a[i];
20       }
21     if(top==0)printf("Yes
");
22     else
23         printf("No
");
24    }
25 }
View Code
原文地址:https://www.cnblogs.com/chujian123/p/3898126.html