/**
*
*/
package Chapter_8;
import java.util.Scanner;
/**
* @author MohnSnow
* @time 2015年5月22日 上午10:21:50
*
*/
public class chapter8_1_5 {
/**
* @param argsmengdx
* -fnst
* @title 字符串压缩功能实现,若压缩变短则替换,否则不替换,例如:aaabb转换为a3b2
*/
private static int countCompression(String str_a) {// 计算转换后的字符长度
char last = str_a.charAt(0);
int count = 1;
int size = 0;
if (str_a == null || str_a.isEmpty())
return size;
for (int i = 1; i < str_a.length(); i++) {
if (last == str_a.charAt(i)) {
count++;
} else {
size += 1 + String.valueOf(count).length();// valueOf()函数转换为String格式,然后计算长度
last = str_a.charAt(i);
count = 1;
}
}
return size + 1 + String.valueOf(count).length();
}
private static int setChar(char[] temp_RAR, char c, int index, int count) {// 计算转换后的字符长度
temp_RAR[index++] = c;
char[] cnt = String.valueOf(count).toCharArray();
for (char x : cnt) {
temp_RAR[index++] = x;
}
return index;
}
@SuppressWarnings("unused")
private static void strRAR_1(String str_a) {// 此方法效率最低
String mystr = "";
char last = str_a.charAt(0);
int count = 1;
for (int i = 1; i < str_a.length(); i++) {
char update = str_a.charAt(i);
if (last == update) {
count++;
} else {
/*
* mystr += last + count; asd 99117d1
* 注意加上“”的区别,类型转换,last位char,count为int,myStr为String
*/
mystr += last + count;
last = str_a.charAt(i);
count = 1;
}
}
System.out.print(mystr + last + count);
}
@SuppressWarnings("unused")
private static void strRAR_2(String str_a) {// StringBuffer方法
if (countCompression(str_a) > str_a.length()) {
System.out.print("beforeRAR: " + str_a);
return;
}
char[] str_arr = str_a.toCharArray();
StringBuffer StrBuf = new StringBuffer();
char apend = str_arr[0];
int count = 1;
StrBuf.append(apend);
for (int i = 1; i < str_a.length(); i++) {
if (apend == str_arr[i]) {
count++;
} else {
apend = str_arr[i];
StrBuf.append(count);
StrBuf.append(apend);
count = 1;
}
}
StrBuf.append(count);
System.out.print("afterRAR: " + StrBuf);
}
private static void strRAR_3(String str_a) {// 构建数组方法
int RARlength = countCompression(str_a);
if (countCompression(str_a) > str_a.length()) {
System.out.print("beforeRAR: " + str_a);
return;
}
char[] temp = str_a.toCharArray();
char[] temp_RAR = new char[RARlength];
char last = temp[0];
// char last = str_a.charAt(0);
int count = 1, index = 0;
temp_RAR[0] = last;
for (int i = 1; i < temp.length; i++) {
if (last == temp[i]) {
count++;
} else {
/*
* temp_RAR[j++] = (char) count; temp_RAR[j++] =
* temp[i];个数大于10,转换出错误,专门写一个方法去处理。StringBuffer会有专门的函数去处理。
*/
index = setChar(temp_RAR, last, index, count);// 专门处理存入temp_RAR数组
last = temp[i];
count = 1;
}
}
setChar(temp_RAR, last, index, count);// 最后一个
System.out.print("AfterRAR: " + String.valueOf(temp_RAR));
}
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
String a = in.nextLine();
System.out.println(countCompression(a));
// strRAR_1(a);// string类型长度固定,+号会重新建立String类型,产生垃圾
strRAR_3(a);
}
}
*
*/
package Chapter_8;
import java.util.Scanner;
/**
* @author MohnSnow
* @time 2015年5月22日 上午10:21:50
*
*/
public class chapter8_1_5 {
/**
* @param argsmengdx
* -fnst
* @title 字符串压缩功能实现,若压缩变短则替换,否则不替换,例如:aaabb转换为a3b2
*/
private static int countCompression(String str_a) {// 计算转换后的字符长度
char last = str_a.charAt(0);
int count = 1;
int size = 0;
if (str_a == null || str_a.isEmpty())
return size;
for (int i = 1; i < str_a.length(); i++) {
if (last == str_a.charAt(i)) {
count++;
} else {
size += 1 + String.valueOf(count).length();// valueOf()函数转换为String格式,然后计算长度
last = str_a.charAt(i);
count = 1;
}
}
return size + 1 + String.valueOf(count).length();
}
private static int setChar(char[] temp_RAR, char c, int index, int count) {// 计算转换后的字符长度
temp_RAR[index++] = c;
char[] cnt = String.valueOf(count).toCharArray();
for (char x : cnt) {
temp_RAR[index++] = x;
}
return index;
}
@SuppressWarnings("unused")
private static void strRAR_1(String str_a) {// 此方法效率最低
String mystr = "";
char last = str_a.charAt(0);
int count = 1;
for (int i = 1; i < str_a.length(); i++) {
char update = str_a.charAt(i);
if (last == update) {
count++;
} else {
/*
* mystr += last + count; asd 99117d1
* 注意加上“”的区别,类型转换,last位char,count为int,myStr为String
*/
mystr += last + count;
last = str_a.charAt(i);
count = 1;
}
}
System.out.print(mystr + last + count);
}
@SuppressWarnings("unused")
private static void strRAR_2(String str_a) {// StringBuffer方法
if (countCompression(str_a) > str_a.length()) {
System.out.print("beforeRAR: " + str_a);
return;
}
char[] str_arr = str_a.toCharArray();
StringBuffer StrBuf = new StringBuffer();
char apend = str_arr[0];
int count = 1;
StrBuf.append(apend);
for (int i = 1; i < str_a.length(); i++) {
if (apend == str_arr[i]) {
count++;
} else {
apend = str_arr[i];
StrBuf.append(count);
StrBuf.append(apend);
count = 1;
}
}
StrBuf.append(count);
System.out.print("afterRAR: " + StrBuf);
}
private static void strRAR_3(String str_a) {// 构建数组方法
int RARlength = countCompression(str_a);
if (countCompression(str_a) > str_a.length()) {
System.out.print("beforeRAR: " + str_a);
return;
}
char[] temp = str_a.toCharArray();
char[] temp_RAR = new char[RARlength];
char last = temp[0];
// char last = str_a.charAt(0);
int count = 1, index = 0;
temp_RAR[0] = last;
for (int i = 1; i < temp.length; i++) {
if (last == temp[i]) {
count++;
} else {
/*
* temp_RAR[j++] = (char) count; temp_RAR[j++] =
* temp[i];个数大于10,转换出错误,专门写一个方法去处理。StringBuffer会有专门的函数去处理。
*/
index = setChar(temp_RAR, last, index, count);// 专门处理存入temp_RAR数组
last = temp[i];
count = 1;
}
}
setChar(temp_RAR, last, index, count);// 最后一个
System.out.print("AfterRAR: " + String.valueOf(temp_RAR));
}
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
String a = in.nextLine();
System.out.println(countCompression(a));
// strRAR_1(a);// string类型长度固定,+号会重新建立String类型,产生垃圾
strRAR_3(a);
}
}