吐泡泡题解
吐泡泡
https://ac.nowcoder.com/acm/problem/15029
栈的基本应用
如果栈中没有元素的话就直接进栈就好了
如果栈顶有元素为o并且当前元素也为o的话,就要出栈并且看看之前的是不是O,如果是的话再出栈,不是得话就让O进栈。
如果都为O的话就出栈,最后合并即可。
import java.math.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.*; public class Main { public static void main(String args[])throws IOException { StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); Scanner input = new Scanner(System.in); Stack<Character> stack = new Stack<>(); while(input.hasNext()) { String s = input.next(); for(int i=0;i<s.length();i++) { if(stack.size()==0) stack.add(s.charAt(i)); else { if(stack.peek()=='o'&&s.charAt(i)=='o') { stack.pop(); if(stack.size()!=0&&stack.peek()=='O') stack.pop(); else{ stack.add('O'); } } else{ if(stack.peek()=='O'&&s.charAt(i)=='O') { stack.pop(); } else{ stack.add(s.charAt(i)); } } } } String s1=""; while(stack.size()!=0) { s1 = stack.pop()+s1; } System.out.println(s1); } } }