Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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.

 

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
 
 
AC Code --Java
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n-- != 0) {
String a = sc.next();
char[] arr = a.toCharArray(); char[] zc = new char[10000];
int[] zs = new int[5000];
int j = 0;
zc[0] = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] == arr[i - 1]) {
zs[j]++;
} else {
zc[++j] = arr[i];
}
} for (int i = 0; i < j + 1; i++) {
if (zs[i] == 0)
System.out.print(zc[i]);
else
System.out.print(zs[i] + 1 + "" + zc[i]);
}
System.out.println();
}
}
}

 C 代码版

#include <stdio.h>

void out(char arr[100]);
int main(void)
{ int n=0,i=0;
char arr1[100][100];
while(scanf("%d",&n)!=EOF)
{
getchar();
for(i=0;i<n;i++)
{
gets(arr1[i]);
} for(i=0;i<n;i++)
{
out(arr1[i]);
}
} return 0;
}
void out(char arr[100])
{
char s[100];
int sum[100];
int i=0,j=0;
s[0]=arr[0];
sum[0]=1;
for(i=1;i<strlen(arr)+1;i++)
{
if(arr[i]==arr[i-1])
sum[j]++;
else
{
s[++j]=arr[i];
sum[j]=1;
}
}
s[j]='\0';
for(i=0;i<strlen(s);i++)
if(sum[i]!=1)
printf("%d%c",sum[i],s[i]);
else
printf("%c",s[i]);
printf("\n");
}

  

hdu 1020 Encoding的更多相关文章

  1. HDU 1020 Encoding POJ 3438 Look and Say

    Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  2. HDU 1020 Encoding 模拟

    Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. HDU 1020 Encoding【连续的计数器重置】

    Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. HDU 1020:Encoding

    pid=1020">Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  5. HDU 1020.Encoding-字符压缩

    Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  6. HDOJ 1020 Encoding

    Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following ...

  7. hdu 1020

    //自信满满地交上去~~but...超时了 #include <iostream> #include <string.h> #include <stdio.h> u ...

  8. HDU 1020(连续同字符统计 **)

    题意是要统计在一段字符串中连续相同的字符,不用再排序,相等但不连续的字符要分开输出,不用合在一起,之前用了桶排序的方法一直 wa,想复杂了. 代码如下: #include <bits/stdc+ ...

  9. HDU字符串基础题(1020,1039,1062,1088,1161,1200,2017)

    并不是很精简,随便改改A过了就没有再简化了. 1020. Problem Description Given a string containing only 'A' - 'Z', we could ...

随机推荐

  1. [转]Spring 注解总结

    原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...

  2. [转]fastjson

    原文地址:http://www.cnblogs.com/zhenmingliu/archive/2011/12/29/2305775.html FastJSON是一个很好的java开源json工具类库 ...

  3. Android 用代码设置Shape,corners,Gradient

    网上查找资料 记录学习 int strokeWidth = 5; // 3dp 边框宽度 int roundRadius = 15; // 8dp 圆角半径 int strokeColor = Col ...

  4. 关于 HTTP 请求头的内容

    HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务器给与响应.就整个网络资源传 ...

  5. Region Representaion and Description

    两类特征 外部特征(external characteristics), 如boundary 内部特征(internal characteristics), 如像素, color, texture. ...

  6. css 选择器样式优先级

    !important > 行内 >id > class >tag >*

  7. SPOJ ORDERSET - Order statistic set

    ORDERSET - Order statistic set   In this problem, you have to maintain a dynamic set of numbers whic ...

  8. Birthday-24

    2013 LEXUS花样滑冰 和母亲在一起,生日快乐!

  9. Mac: Jdk版本切换

    通过命令’jdk6′, ‘jdk7′,’jdk8′轻松切换到对应的Java版本: 1.首先安装所有的JDk:* Mac自带了的JDK6,安装在目录:/System/Library/Java/JavaV ...

  10. [iOS 图像处理相关]

    //使用CGImage获取并修改图片像素 #define Mask8(x) ( (x) & 0xFF ) #define R(x) ( Mask8(x) ) #define G(x) ( Ma ...