题解 | #万万没想到之聪明的编辑#
万万没想到之聪明的编辑
http://www.nowcoder.com/questionTerminal/42852fd7045c442192fa89404ab42e92
主要思路,就是分两次处理, 利用数组标记是否 被 删除 过, 最开始想一次处理完, 发现不行.
import java.util.Scanner; /** * @Author Fizz Pu * @Date 2021/6/19 下午4:10 * @Version 1.0 * 失之毫厘,缪之千里! */ public class Main1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for(int q = 0; q < n; q ++) { String data = scanner.next(); char[] arrays = data.toCharArray(); int len = arrays.length; if (len < 3) { System.out.println(data); continue; } // 第一次处理 AAA 型 int[] delete = new int[len]; // 1为删除 int i = 0, j = 1, k = 2; int count = len; // 字符串的个数 while (k < len) { if (arrays[i] == arrays[j] && arrays[i] == arrays[k]) { delete[k] = 1; count--; } i ++; j ++; k ++; } // 生成新的数组 // 在第一次的基础上处理AABB型 char[] newStr = new char[count]; i = 0; for (int m = 0; m < len; m++) { if (delete[m] == 0) { newStr[i++] = arrays[m]; } delete[m] = 0; } i = 0; j = 1; k = 2; while (k < count - 1) { if (newStr[i] == newStr[j] && newStr[k] == newStr[k+1]) { delete[k] = 1; i = k + 1; j = i + 1; k = j + 1; } i++; j++; k++; } for (int m = 0; m < count; m++) { if (delete[m] == 0) { System.out.print(newStr[m]); } } System.out.println(); } } }