Description 给你一个由大写字母组成的组成的字符串,你可以用如下规则对其进行编码: 1、 包含K个相同字母的连续字符串可以用KX表示,其中X是相同的字母。 2、 如果K为1,不输出K Input 输入有多组,直到文件结束。每组一个字符串,长度为10000以内 Output 输出编码后的字符串。 Sample Input ABC ABBCCC Output ABC A2B3C
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner reader = new Scanner(System.in); int a[] = new int[10010];//字母数量 String str; char b[] = new char[10010];//字母类别 int i, j, m; while (reader.hasNext()) { str = reader.nextLine(); m = 0; for (i = 0; i < str.length(); i++) { b[m] = str.charAt(i); a[m] = 1; for (j = i + 1; j < str.length(); j++) { if (b[m] != str.charAt(j)) break; else a[m]++; } i = j - 1;//注意比较完同类字母后,i要返回新字母的位置 m++; } for (i = 0; i < m; i++) { if (a[i] != 1) System.out.print(a[i]); System.out.print(b[i]); } System.out.println(); } } }