HDU-1020-Encoding(Java && 弱水三千)
时间:2015-05-17 09:27:31
收藏:0
阅读:135
Encoding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 31603 Accepted Submission(s): 14020
Problem Description
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.
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 10000.
Output
For each test case, output the encoded string in a line.
Sample Input
2 ABC ABBCCC
Sample Output
ABC A2B3C
Author
ZHANG Zheng
Recommend
很简单的水题,我不会告诉你我是无聊才来切水题的!
题目的主要意思就是:让你求一个字符串里面连续的不同字符的个数,注意是连续的字符!
所以AABCCA的结果是2AB2CA而不是3AB2C!就只有这一个坑点了,其他没有了......
另外注意如果字符是1前面不输出1......
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int a[] = new int[10005];
int n = input.nextInt();
input.nextLine();
for (int i = 0; i < n; i++)
{
int count = 1;
String str = input.nextLine();
char c[] = str.toCharArray();
for (int j = 1; j < c.length; j++)
{
if (c[j] == c[j - 1])
{
++count;
}
else
{
if (count == 1)
{
System.out.print(c[j - 1]);
}
else
{
System.out.print(count + "" + c[j - 1]);
count = 1;
}
}
if (j == c.length - 1)
{
if (count == 1)
{
System.out.print(c[j]);
}
else
{
System.out.print(count + "" + c[j]);
count = 1;
}
}
}
System.out.println();
}
}
}
评论(0)