HDOJ 1020 Encoding
Problem Description
Given a string containing only ‘A’ - ‘Z’, we could encode it using the following method:
Each sub-string containing k same characters should be encoded to “kX” where “X” is the only character in this sub-string.
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的更多相关文章
- 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 ...
- HDU 1020 Encoding 模拟
Encoding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU 1020 Encoding【连续的计数器重置】
Encoding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu 1020 Encoding
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description Given a ...
- HDU 1020:Encoding
pid=1020">Encoding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- string黑科技
1. string对象的定义和初始化以及读写 string s1; 默认构造函数,s1为空串string s2(s1); 将s2初始化为s1的一个副本string s3("valuee&qu ...
- C++STL之string (转)
在学习c++STL中的string,在这里做个笔记,以供自己以后翻阅和初学者参考. 1:string对象的定义和初始化以及读写 string s1; 默认构造函数,s1为空串 string ...
- C++STL之String
本文直接转载,非原创!仅记录供自己学习之用. 出处:http://blog.csdn.net/y990041769/article/details/8763366 在学习c++STL中的string, ...
- 杭电hdoj题目分类
HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...
随机推荐
- When does layoutSubviews get called?
转自:http://blog.logichigh.com/2011/03/16/when-does-layoutsubviews-get-called/ It’s important to optim ...
- SpringMVC11文件上传
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- HDU 2639 (01背包第k优解)
/* 01背包第k优解问题 f[i][j][k] 前i个物品体积为j的第k优解 对于每次的ij状态 记下之前的两种状态 i-1 j-w[i] (选i) i-1 j (不选i) 分别k个 然后归并排序并 ...
- codevs3304水果姐逛水果街
/* 线段树开到*4 *4 *4 *4 ! 维护 4个值 区间最大值 区间最小值 从左往右跑最大收益 从右往左跑最大收益 */ #include<iostream> #include< ...
- 启动php-fpm报错:please specify user and group other than root
安装好PHP之后启动报错: 启动php-fpm报错:please specify user and group other than root, pool 'default 修改 php-fpm.co ...
- 使用Instant Client配置PL/SQL Developer
之前使用PL/SQL Developer都是直接在本机安装完整版的Oracle Database,一是省事,二是可以在本机做一些demo测试:最近换了台电脑,感觉Instant Client更简单一些 ...
- 困扰:C#.net 连接Oracle11g 不报错但是在connection时出现 ServerVersion 引发了“System.InvalidOperationException”类型的异常
今天在使用VS2008 32位 连接 64位的Oracle11g的数据库时出现 “conn.ServerVersion”引发了“System.InvalidOperationException”类型的 ...
- 【转】 iOS如何实现表格的折叠效果?
原文 : http://blog.csdn.net/youcanping2008/article/details/9202167 一.实现原理:就是在点击表格组头视图的时候,如果该表格视图的组展开了 ...
- javascript 原生 cookie 处理
来自网络! function getCookie(name) { var start = document.cookie.indexOf(name + "="); var len ...
- RedHat9上安装jdk
1.先在windows下载jdk:jdk-6-dlj-linux-i586.bin 2.用ftp上传给linux下 3.chmod 777 jdk-6-dlj-linux-i586.bin 4.将jd ...