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.

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

题意:按照字符串的顺序,输出字符的个数和字符。

如果字符出现次数为1次,只要输出原字符。

如果输入为:ABBCCCBBB

输出为:A2B3C3B

而不是:A5B3C

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt(); while (t-- > 0) {
String strs = sc.next();
// System.out.println(strs+"=strs");
boolean isSee[] = new boolean[strs.length()];
for (int i = 1; i < isSee.length; i++) {
isSee[i] = false;
}
int sum = 0;
boolean isLast=false;
for (int i = 0; i < strs.length()-1; i++) {
if(strs.charAt(i)==strs.charAt(i+1)) {
isSee[i]=true;
isSee[i+1]=true;
sum=sum+1;
}else{
sum=sum+1;
isSee[i]=false;
} if(!isSee[i]){
if(sum==1){
System.out.print(strs.charAt(i));
}else{
System.out.print(""+sum+strs.charAt(i));
}
} if(!isSee[i]){
sum=0;
} } if(isSee[strs.length()-1]){
System.out.print(""+(sum+1)+strs.charAt(strs.length()-1));
}else{
System.out.print(strs.charAt(strs.length()-1));
}
System.out.println();
}
} }

HDOJ 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

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

  5. HDU 1020:Encoding

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

  6. string黑科技

    1. string对象的定义和初始化以及读写 string s1; 默认构造函数,s1为空串string s2(s1); 将s2初始化为s1的一个副本string s3("valuee&qu ...

  7. C++STL之string (转)

    在学习c++STL中的string,在这里做个笔记,以供自己以后翻阅和初学者参考. 1:string对象的定义和初始化以及读写 string s1;      默认构造函数,s1为空串 string ...

  8. C++STL之String

    本文直接转载,非原创!仅记录供自己学习之用. 出处:http://blog.csdn.net/y990041769/article/details/8763366 在学习c++STL中的string, ...

  9. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...

随机推荐

  1. 关于Linux的缓存内存 Cache Memory详解<转>

    转自 http://www.ha97.com/4337.html PS:前天有童鞋问我,为啥我的Linux系统没运行多少程序,显示的可用内存这么少?其实Linux与Win的内存管理不同,会尽量缓存内存 ...

  2. jQuery中在当前页面弹出一个新的界面

    W.$.dialog({ content:'url:wswgrkbillController.do?snh&id='+b+'&bh='+c+'&ck='+d+'&sl= ...

  3. MySQL存储过程详解 mysql 存储过程(二)

    mysql存储过程详解 1.      存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL ...

  4. 11、SQL Server 视图、数据库快照

    SQL Server 视图 什么是视图? 视图是一个虚拟的表,内容源于查询的结果集.只有当视图上建立了索引后,才会具体化. 视图可以筛选和处理数据,而不是直接访问基础表.如:创建一个视图,只展示源表中 ...

  5. IIS 配置好了,为什么网站打开一片空白?

    方法如下: 进入:控制面板 - 卸载程序 - 打开或关闭Windows功能 如果访问任何不存在页面或页面出错时空白: Internet 信息服务 - 万维网服务 - 常见 HTTP 功能 - HTTP ...

  6. css控制文字长度,超出长度显示...

    css控制文字长度,超出长度显示... .style { max-width: 165px; overflow: hidden; white-space: nowrap; text-overflow: ...

  7. SQL中 patindex函数的用法

    语法格式:PATINDEX ( '%pattern%' , expression ) 返回pattern字符串在表达式expression里第一次出现的位置,起始值从1开始算. pattern字符串在 ...

  8. Protostuff自定义序列化(Delegate)解析

    背景 在使用Protostuff进行序列化的时候,不幸地遇到了一个问题,就是Timestamp作为字段的时候,转换出现问题,通过Protostuff转换后的结果都是1970-01-01 08:00:0 ...

  9. protocol buffer VS 2013编译出错

    protocol buffer 在VS2013编译会出现以下错误. 解决办法 把宏加上, 问题解决. 注: 该错误只出现在Debug版本.

  10. 使用grunt压缩css是能否设置background-size不压缩进去呢?否则ie8不能识别

    .index-bg{ background:url(img/index-bg-t.5344b19d.jpg) center center/cover no-repeat } 比如上面这样ie8不能识别 ...