部分正确,三个测试点错误 ,暂未解决
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* 1078
* @author quchu
* @date 2019/10/23
*/
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String actionStr=br.readLine();//获取操作类型
char[] inputStr=br.readLine().toCharArray();
StringBuffer result=new StringBuffer();//用于保存操作结果
if(actionStr.equals("C")){
//进行压缩操作
for (int i = 0; i <inputStr.length-1; i++) {
if((inputStr[i]>='A'&&inputStr[i]<='z') ||inputStr[i]==' '){
//进行压缩操作
int count=1;
while(i<inputStr.length-1&&inputStr[i]==inputStr[i+1]){
count++;
i++;
}
if(count>1) {
result.append(String.valueOf(count));
result.append(inputStr[i]);
}else{
result.append(inputStr[i]);
}
}
if(i==inputStr.length-2 && inputStr[i]!=inputStr[i+1]){
result.append(inputStr[i+1]);
break;
}
}
}else if(actionStr.equals("D")){
//进行解压操作,进行解析操作
for (int i = 0; i < inputStr.length-1; i++) {
StringBuffer tempNumStr=new StringBuffer();
if(inputStr[i]>='0'&&inputStr[i]<='9'){
//是数字的情况下
tempNumStr.append(inputStr[i]);
while(i<inputStr.length-1 && inputStr[i+1]>='0'&&inputStr[i+1]<='9'){
tempNumStr.append(inputStr[i++]);
}
int tempNum=Integer.parseInt(tempNumStr.toString());//获取到需要循环的内容
for (int j = 0; j < tempNum; j++) {
result.append(inputStr[i+1]);//此时i+1为对应的字符
}
//循环完当前的字符,
i++;
continue;
}else{
//不是数字的情况下
result.append(inputStr[i]);
}
}
}
System.out.println(result.toString());
}
}