Encoding

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1020

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 55038    Accepted Submission(s):
24576

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
 
Author
ZHANG Zheng

可以用暴力求解法求解。

JAVA代码如下:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner inScanner = new Scanner(System.in);
int num = inScanner.nextInt();
inScanner.nextLine();
while(num-->0) {
String string = inScanner.nextLine();
string+="a";//这个要注意,防止接下来的统计时会有溢出的。
int sum = 1;
for(int i = 0;i < string.length()-1;i++) {
if(string.charAt(i) == string.charAt(i+1)) {

sum++;
}
else {
if(sum==1) {
System.out.print(string.charAt(i));
}
else {
System.out.print(sum + "" + string.charAt(i));
sum = 1;
}
}
}
System.out.println();//这个很迷,我之前由于用了String.out.print("\n");而WA了好几次。。。。。。

}
}
}

C++代码:

#include<iostream>
#include<string>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string a;
cin>>a;
int len=a.length();
int sum=;
for(int i=;i<len;i++)
{
if(a[i]==a[i+])
{
sum++;
}
else
{
if(sum==)
{
cout<<a[i];
sum=;
}
else
{
cout<<sum<<a[i];
sum=;
}
}
}
cout<<endl;
}
return ;
}

(暴力求解)Encoding HDU1020的更多相关文章

  1. POJ 1562(L - 暴力求解、DFS)

    油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...

  2. 逆向暴力求解 538.D Weird Chess

    11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ...

  3. 隐型马尔科夫模型(HMM)向前算法实例讲解(暴力求解+代码实现)---盒子模型

    先来解释一下HMM的向前算法: 前向后向算法是前向算法和后向算法的统称,这两个算法都可以用来求HMM观测序列的概率.我们先来看看前向算法是如何求解这个问题的. 前向算法本质上属于动态规划的算法,也就是 ...

  4. BestCoder Round #79 (div.2)-jrMz and angles,,暴力求解~

    jrMz and angle       Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Other ...

  5. hdu6570Wave (暴力求解)

    Problem Description Avin is studying series. A series is called "wave" if the following co ...

  6. <字符串匹配>KMP算法为何比暴力求解的时间复杂度更低?

    str表示文本串,m表示模式串; str[i+j] 和 m[j] 是正在进行匹配的字符; KMP的时间复杂度是O(m+n)  ,  暴力求解的时间复杂度是O(m*n) KMP利用了B[0:j]和A[i ...

  7. HDU 4462 Scaring the Birds (暴力求解,二进制法)

    题意:给定一个 n*n的矩阵,在一些位置放上稻草人,每个稻草人的范围是一定,问你最少几个能覆盖整个矩阵. 析:稻草人最多才10个,所以考虑暴力,然后利用二进制法,很容易求解,并且时间很少0ms,注意有 ...

  8. ZOJ 2856 Happy Life 暴力求解

    因为是Special Judge 的题目,只要输出正确答案即可,不唯一 暴力力求解, 只要每次改变 happiness 值为负的人的符号即可. 如果计算出当前人的 happiness 值为负,那么将其 ...

  9. HDU 2601An easy problem-素数的运用,暴力求解

    id=17433" target="_blank" style="color:blue; text-decoration:none">An ea ...

随机推荐

  1. 一、使用Navicat连接阿里云服务器宝塔面板里创建的数据库

    一.数据库配置连接  (通过新增用户的方式)

  2. Python——WeRobot(微信公众号开发)

    模板消息接口 ''' 使用规则 1.所有服务号都可以在功能->添加功能插件处看到申请模板消息功能的入口,但只有认证后的服务号才可以申请模板消息的使用权限并获得该权限: 2.需要选择公众账号服务所 ...

  3. oracle ceil函数

    ceil和floor函数在一些业务数据的时候,有时还是很有用的. ceil(n) 取大于等于数值n的最小整数: floor(n)取小于等于数值n的最大整数 如下例子 SQL> select ce ...

  4. Overrid Equals Defined Operator

    public class Common { public override int GetHashCode() { return base.GetHashCode(); } public overri ...

  5. Python中数字之间的进制转换

    Python中的数据转换 在python中可以通过内置方法进行相应的进制转换,但需记得转化成非十进制时,都会将数字转化成字符串 转化成二进制 a = 10 #声明数字,默认十进制 b = bin(a) ...

  6. git在Linux下的安装

    参考:https://git-scm.com/book/zh/v1/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git Git 的工作需要调用 curl,zlib,op ...

  7. python3 集合set

    set是一种集合的数据类型,使用{}表示 集合中元素是无序的,并且不可重复,集合最重要的作用就是可以去重 set是不可哈希的,set中的元素必须是可哈希的 可以切片,可以迭代 交集.并集.差集.对称差 ...

  8. 通过pycharm将代码push到远程仓库

    现在使用pycharm作为python编辑器的人还是不少,而且,也可以通过pycharm将代码push到远程仓库. 步骤见下面截图: 填上远程仓库地址及克隆到本地的目录 输入远程仓库的账号和密码 修改 ...

  9. HDU 6319 Problem A. Ascending Rating(单调队列)

    要求一个区间内的最大值和每次数过去最大值更新的次数,然后求每次的这个值异或 i 的总和. 这个序列一共有n个数,前k个直接给出来,从k+1到n个数用公式计算出来. 因为要最大值,所以就要用到单调队列, ...

  10. [ZJOI2016]小星星&[SHOI2016]黑暗前的幻想乡(容斥)

    这两道题思路比较像,所以把他们放到一块. [ZJOI2016]小星星 题目描述 小Y是一个心灵手巧的女孩子,她喜欢手工制作一些小饰品.她有n颗小星星,用m条彩色的细线串了起来,每条细线连着两颗小星星. ...