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. Python——线程1

    多线程并发 from threading import Thread import time #多线程并发 def func(n): time.sleep(1) print(n) for i in r ...

  2. docker 搭建简易仓库registry

    下载仓库镜像: docker pull  registry:2 运行仓库库镜像: docker run -d  -p 5000:5000  -v /usr/local/registry:/var/li ...

  3. kubernetes 构架

  4. vscode——配置git的path

    设置 打开设置 搜索配置 复制Json文本 编辑配置 粘贴刚才复制的json文本,并将自己git的地址写好,保存即可 完整配置 { "workbench.colorTheme": ...

  5. HDU1659-GCD-容斥原理

    从1-a和1-b种选两个数xy,计算出令gcd(x,y)=k的xy的对数. 对于每一个i∈[1,b]使用solve(i,n)函数解决有几个j∈[1,n]使gcd(x,y)=k.然后累加solve(i, ...

  6. HBase 清空表数据

    public int clearTableByTableName(String tableName) throws Exception { logger.debug("======InitH ...

  7. python打印log重复问题

    本博客转载于:http://www.cnblogs.com/huang-yc/p/9209096.html,写得真不错 浅析python日志重复输出问题 目录 问题起源: 问题解析 解决办法 1.改名 ...

  8. Log Parser Studio 分析 IIS 日志

    Log Parser Studio 分析 IIS 日志 来源 https://www.cnblogs.com/lonelyxmas/p/8671336.html 软件下载地址: Log Parser ...

  9. 基于Thinkphp5.0 小程序登录插件应用

    资源连接: wulongtao/think-wxminihelper 具体怎么安装,不介绍了,有不懂再问我吧: 主要重点如下: wepy:index.wpy this.$parent.getUserI ...

  10. Linux 源码安装 Python3

    下载源码包https://www.python.org/downloads/ 解压(以3.64版本为例)wget https://www.python.org/ftp/python/3.6.4/Pyt ...