题解 | 写的忒墨迹,暴力模拟
万万没想到之聪明的编辑
http://www.nowcoder.com/questionTerminal/42852fd7045c442192fa89404ab42e92
import java.util.*;
import java.lang.*;
public class Main{
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
for(int i = 0; i < n; i++){
String res = parse(cin.next());
System.out.println(res);
}
}
public static String parse(String word){
int cnt = 0;
StringBuilder sb = new StringBuilder();
int n = word.length();
sb.append(word.charAt(0));
for(int i = 1; i < n; i++){
if(cnt == 1){
if(word.charAt(i - 1) != word.charAt(i)){
sb.append(word.charAt(i));
// mark this condition
if(i + 1 < n && word.charAt(i) != word.charAt(i + 1)){
cnt = 0;
}
}else{
while(i < n && word.charAt(i - 1) == word.charAt(i)){
i++;
}
// mark this
i--;
cnt = 0;
}
}else{
sb.append(word.charAt(i));
if(word.charAt(i) == word.charAt(i - 1)){
cnt = 1;
// mark this condition
while(i < n && word.charAt(i) == word.charAt(i - 1)){
i++;
}
// mark this
i--;
}
}
// System.out.println(sb);
}
return sb.toString();
}
}
查看10道真题和解析