ZOJ2478(Encoding)
Given a string containing only 'A' - 'Z', we could encode it using the following method:
1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.
2. If the length of the sub-string is 1, '1' should be ignored.
Input
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only 'A' - 'Z' and the length is less than 100.
Output
For each test case, output the encoded string in a line.
Sample Input
2
ABC
ABBCCC
Sample Output
ABC
A2B3C
//2009-05-12 08:54:05 Accepted 2478 Java 0 122
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
String a;
int T, i, cnt;
Scanner cin = new Scanner(System.in);
T = cin.nextInt();
for(int j = 0; j < T; j++)
{
a = cin.next();
for(i = 0; i < a.length();)
{
cnt = 1;
while((i + 1 < a.length()) && a.charAt(i) == a.charAt(i + 1))
{
cnt++;
i++;
}
if(cnt > 1)
System.out.print(cnt);
System.out.print(a.charAt(i));
i++;
}
System.out.print('\n');
}
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
String a;
int T, i, cnt;
Scanner cin = new Scanner(System.in);
T = cin.nextInt();
for(int j = 0; j < T; j++)
{
a = cin.next();
for(i = 0; i < a.length();)
{
cnt = 1;
while((i + 1 < a.length()) && a.charAt(i) == a.charAt(i + 1))
{
cnt++;
i++;
}
if(cnt > 1)
System.out.print(cnt);
System.out.print(a.charAt(i));
i++;
}
System.out.print('\n');
}
}
}