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 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...
随机推荐
- [转] 怎样快速而优雅地遍历 JavaScript 数组
我们一般用循环来遍历数组,而循环一直是 JavaScript 性能问题的常见来源,有时循环用得不好会严重降低代码的运行速度.例如,遍历数组时,我们会很自然地写出下面这种代码: // 未优化的代码1 v ...
- C++对象模型学习笔记
1. 全局变量是如何初始化的 //global var A a; int main() { cout<<a<<endl; ; } 如上述例子,全局变量a是在main()函数之前 ...
- NYOJ 1107 最高的奖励(贪心+优先队列)
最高的奖励 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 请问:挖掘机技术哪家强?AC了告诉你! 给你N(N<=3*10^4)个任务,每个任务有一个截止完成时 ...
- Modernizr——为HTML5和CSS3而生!
原文地址:http://www.alistapart.com/articles/taking-advantage-of-html5-and-css3-with-modernizr/ 堂主译文地址:ht ...
- java学习第一阶段——面向对象
你聪明有人会说你心机重, 你靠的是努力有人会说你运气好, 你说自己天生乐观有人会说你虚假, 有时候, 你明明就是一杯白水, 却被人硬生生逼成了满肚子憋屈的碳酸饮料. 人一生要遇见太多人, 即使有些话字 ...
- Android开发手记(16) 数据存储一 SharedPreferences
Android为数据存储提供了五种方式: 1.SharedPreferences 2.文件存储 3.SQLite数据库 4.ContentProvider 5.网络存储 SharedPreferenc ...
- android对象序列化Parcelable浅析
一.android序列化简介 我们已经知道在Android使用Intent/Bindler进行IPC传输数据时,需要将对象进行序列化. JAVA原本已经提供了Serializable接口来实现序列化, ...
- 使用mailto在网页中链接Email地址
<a>标签还有一个作用是可以链接Email地址,使用mailto能让访问者便捷向网站管理者发送电子邮件.我们还可以利用mailto做许多其它事情.下面一一进行讲解,请看详细图示: 注意:如 ...
- 疯狂学习java web2(css)
CSS应该是样式描述的意思,定义如下: 什么是 CSS? CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常存储在样式表中 把样式添加到 ...
- socket (转,吴秦,http://www.cnblogs.com/skynet/archive/2010/12/12/1903949.html)
Linux Socket编程(不限Linux)2010-12-12 21:58 by 吴秦 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页时,浏览器的进程怎么与web ...