CodeForces 343B Alternating Current :两根绳子上下绕在一起,问拉住上下绳子能不能分开:思维+栈

图见:http://codeforces.com/problemset/problem/343/B

巧妙!

将红上蓝下记为1,红下蓝上记为-1,压栈,相同相消,最后栈中无元素即可以分开=

 1 #include<stdio.h>
 2 #include<stack>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6 char str[100005];
 7 stack<char>s;
 8 int main()
 9 {
10   int i,len;
11   scanf("%s",str);
12   len=strlen(str);
13   while (!s.empty()) s.pop();
14   for (i=0;i<len;i++)
15     if (s.empty()) s.push(str[i]);
16     else{
17       if (s.top()==str[i]) s.pop();
18       else s.push(str[i]);
19     }
20   if (s.empty()) printf("Yes\n");
21   else printf("No\n");
22 }
View Code

posted on 2015-03-26 11:51  xiao_xin  阅读(383)  评论(0编辑  收藏  举报

导航