题解 | #吐泡泡#
吐泡泡
https://ac.nowcoder.com/acm/problem/15029
思路
法1:手写栈
Code
#include <bits/stdc++.h> using namespace std; const int N = 110; char stk[N]; string s; int hh; bool check(int h,int i){ if(stk[i]=='o'&&stk[h]=='o') return true; if(stk[i]=='O'&&stk[h]=='O') return true; return false; } int main(){ while(cin>> s){ hh=0; for(int i=0;i<s.size();i++){ stk[++hh]=s[i]; while(hh>1&&check(hh,hh-1)){ if(stk[hh]=='o') hh--,stk[hh]='O'; else hh-=2; } } for(int i=1;i<=hh;i++) cout<<stk[i]; puts(""); // hh不再是0,所以需要重新置零 } return 0; }
法2:stl 栈
Code
#include <bits/stdc++.h> using namespace std; const int N = 110; string s; bool check(char c1,char c2){ if(c1==c2) return true; return false; } int main(){ while(cin>> s){ stack<char> stk; for(int i=0;i<s.size();i++){ stk.push(s[i]); while(stk.size()>=2){ char c1=stk.top(); stk.pop(); char c2=stk.top(); stk.pop(); stk.push(c2); stk.push(c1); if(!check(c1,c2)) break; if(c1=='o'){ stk.pop(); stk.pop(); stk.push('O'); } else stk.pop(),stk.pop(); } } string res; while(stk.size()){ res+=stk.top(); stk.pop(); } reverse(res.begin(),res.end()); cout<<res<<endl; // hh不再是0,所以需要重新置零 } return 0; }