杭电ACM OJ 1020 Encoding 输入一串字符 判断每种字符个数 我犯了个错误定义数组长度的错误
2017-11-18 01:39
549 查看
Encoding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49076 Accepted Submission(s): 21875
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
翻译:不用翻译了
核心:用java的map做,真是简单,当然c,c++做也是一点都不难
错误:我犯了个错误:错误定义了数组长度,想当然地定义了100,然后添加了94个空的‘’到map里去,然后输出的时候就莫名其妙了。
代码:public class Encoding1020 {
char[] mChars;
Encoding1020(char...chars) {
mChars = new char[chars.length];
System.arraycopy(chars, 0, this.mChars, 0, chars.length);
}
private Map<Character, Integer> sort(char[] chars) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < chars.length; i ++) {
char c = chars[i];
if (!map.containsKey(c)) {
map.put(c, 1);
} else {
int num = map.get(c);
map.put(c, ++num);
}
}
return map;
}
public static void main(String[] args) throws Exception {
Encoding1020 encoding1020 = new Encoding1020('A','B','B','C','C','C');
Map<Character, Integer> map = encoding1020.sort(encoding1020.mChars);
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
char key = entry.getKey();
int value = entry.getValue();
if (value != 1) {
System.out.print(value + "");
}
System.out.print(key + "");
}
}
}
相关文章推荐
- 在结构体最后定义一个长度为0的字符数组(技巧)
- 可遇不可求的Question之反序列化时出现“base-64 字符数组的无效长度”错误提示篇
- 实验9-2 //编程判断输入的一串字符是否为“回文”。所谓“回文”,是指顺读和倒读都一样的字符串。如“XYZYX”和“xyzzyx”都是“回文”。要求使用指针变量判断字符串是否是回文。
- 判断输入的一串字符{}()【】是否按照正确格式书写
- 在一个长度为10的整形数组中,已经放置好了任意10个整数。要求用户任意输入 一个整数,判断该数字是否在数组中。如果在,删除数组中该元素,并把后面的 元素往前挪。(让空出的位置在最后)
- 运用递归输入一串任意长度的字符并输出
- java初探秘之判断输入的一串字符是否全为小写字母
- scanf 如果输入 %s 会出现即使输入的字符长度长于定义的长度也会执行
- 输入一个英文单词,判断该单词是否是回文,要求用字符数组实现
- 【JS】JS判断字符输入个数(数字英文长度记为1,中文记为2,超过长度自动截取)
- 文本框内默认提示————请输入用户姓名(字体灰色),要求: (1)当文本框获得焦点时,默认提示消失 (2)当文本框失去焦点时,如果没有输入新内容,那么则显示原来的灰色字体内容; 如果有新内容输入则判断,如果长度小于10,则提示“姓名长度应该大于10个字符”
- ASP.NET错误:输入数组长度大于此表中的列数
- jquery判断字符输入个数(数字英文长度记为1,中文记为2,超过长度自动截取)
- 定义一个长度为10的整型数组,循环输入10个整数。 并遍历打印该数组
- 输入一串字符,字符个数不超过100,且以“.”结束。 判断它们是否构成回文。
- js脚本系列(1) JS判断输入字符串长度(汉字算两个字符,字母数字算一个)
- base-64字符数组的无效长度”错误解决方案
- JS判断输入字符串长度(汉字算两个字符,字母数字算一个)
- js判断输入字符串长度(汉字算两个字符,字母数字算一个)