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 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...
随机推荐
- C#读取Word文档内容代码
首先要添加引用com组件:然后引用: using Word = Microsoft.Office.Interop.Word; 获取内容: /// /// 读取 word文档 返回内容 /// //// ...
- Windows Azure上的Odoo(OpenERP)
OpenERP 改名为 Odoo 了,感觉名字怪怪的.Windows Azure也进入国内了,学习了一段时间的Azure,把它门结合在一起搞搞吧!本系列文章不涉及开发,纯属环境搭建及Odoo 系统功能 ...
- java.lang.UnsupportedClassVersionError: Bad version number in .class file
java.lang.UnsupportedClassVersionError: Bad version number in .class file造成这种过错是ni的支撑Tomcat运行的JDK版本与 ...
- knockoutjs关键点
<p>Your value: <input data-bind="value: someValue, valueUpdate: 'afterkeydown'"/& ...
- (转)织梦dedecms模板。如何让type='image'和不带type='image'的文章同时出现在列表里。
“节日歌圩”栏目是有内容的,但是文章没有缩略图所以没有在频道首页显示出来,我现在想要有缩略图的文章自然显示,没有缩略图的文章也能出现标题列表(依然按照一行4个标题,可以用一张“无缩略图”的图片来代替缩 ...
- Swift - 10 - assert(断言)
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- 模仿qq音乐播放字母效果
html <div class="cont"> <ul class="cont_ul" id="cont_ul"> ...
- C++ Primer 5th 第15章 面向对象程序设计
面向对象程序设计的核心思想是:数据抽象.继承和动态绑定. 数据抽象:将类的接口与实现分离: 继承:定义相似类型并对相似关系建模: 动态绑定:一定程度上上忽略相似类型间的区别,用同一方式使用它们. 1. ...
- 导出页面文档(只在IE8下测试过)
之前说过一篇关于打印的方法,就顺便也看了一下导出,但是该方法需要用户更改浏览器的安全级别设置,因此并不十分推荐,大家如真有需要可以参考一下ZeroClipboard这款插件,我有时间也会去学习一下并贴 ...
- 删除左右两边的空格trim
js中却没有trim()/ltrim()和rtrim()内置方法,所以需要自己写 写成类的方法格式如下:(str.trim();) <script language="javascri ...