点击消除 题解
点击消除
https://ac.nowcoder.com/acm/contest/5600/E
这里我是用了一个num数组来记录对应位置的字符是否保留。
然后往后找找到两个相同的字符,然后用双指针往两边展开,跳过已经被删除的字符。
最后只需要把还保留的字符输出即可
import java.util.*; import java.math.*; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.PrintWriter; public class Main { public static HashMap<Integer,Long>map = new HashMap<>(); 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)); in.nextToken(); String s = in.sval; int num[] = new int[s.length()]; Arrays.fill(num,1); int l=0,r=0; String s1=""; for(int i=1;i<s.length();i++) { if(num[i]!=0&&num[i-1]!=0&&s.charAt(i)==s.charAt(i-1)) { r = i; l = i-1; while(l>=0&&r<s.length()&&s.charAt(l)==s.charAt(r)) { num[l] = 0; num[r] = 0; l--;r++; while(l>=0&&num[l]==0) l--; while(r<s.length()&&num[r]==0) r++; } } } for(int i=0;i<s.length();i++) { if(num[i]!=0) s1+=s.charAt(i); } if(s1.length()==0) out.print(0); else{ out.print(s1); } out.flush(); } }