ACM1020:Encoding
2024-08-27 06:14:08
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.
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
ABBCCC
Sample Output
ABC
A2B3C
------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
int main()
{
int N, length, count;
char str[10001];
scanf("%d", &N);
while (N)
{
scanf("%s", str);
length = strlen(str);
count = 1;
for (int i = 1; i <= length; i++)
{
if (str[i] == str[i - 1])
count++;
else
{
if (count == 1)
printf("%c", str[i - 1]);
else
{
printf("%d%c", count, str[i - 1]);
count = 1;
}
}
}
printf("\n");
N--;
}
return 0;
}
最新文章
- [vijos P1083] 小白逛公园
- tz2txt的安装与使用
- 每天一个linux命令(41):at命令
- ECshop 每个数据库表结构说明
- Ununtu 12.04 gedit安装插件Source Code Browser
- JQUERY选择和操作DOM元素(利用正则表达式的方法匹配字符串中的一部分)
- 初识golang
- R系列:分词、去停用词、画词云(词云形状可自定义)
- HTML DOM应用案例2
- (关于数据传输安全)SSH协议
- Jmeter之Constant Timer与constant throughput timer的区别(转)
- NEST - 编写查询
- 清除 System.Web.Caching.Cache 以";xxx";开头的缓存
- 【转】web应用缓慢故障分析
- linux系统下添加新硬盘的方法详解
- 关《我是IT小小鸟》有感
- UNIX网络编程读书笔记:TCP输出、UDP输出和SCTP输出
- JavaScript indexof方法
- 【OBJC】数字转中文大写
- bzoj3502[PA2012]Tanie Linie(最大k区间和)