您的位置:首页 > 其它

HOJ 1020

2011-04-19 12:03 411 查看

Encoding

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


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

Recommend
JGShining
这题真的是让我费解很久,后面查了查大家对这题的说法,主要是,统计字符的时候,如果是连续的字符,那就需要统计连续相同的个数,而不是全局的进行统计全部字符中相同字符的个数
#include<stdio.h>
#include<string.h>/*
-----------------------------------------------------------------------------------
我的代码,也是一直WA的代码,
主要是通过计算字符串中出现的
所有相同字符的个数,并且从ASCII码
表进行从小到大的输出

using namespace std;

int main()
{
int  b, a[50];
int tcase, i, len;
char letter[ 10000 ];
char c[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
cin >> tcase;
while( tcase -- )
{
for( i =0 ;i < 50;i ++)
a[i] = 0;
cin >> letter;
len = strlen ( letter );
for ( i = 0; i < len; i ++ )
{
b = letter[i];
a[ b - '0'] ++ ;
}
for ( i = 17; i < 43;i ++ )
{
if ( a[i] == 0)
continue;
else if( a[i] == 1)
cout << c[i-17];
else
cout << a[i] << c[i-17];
}
cout << endl;
}
return 0;
}
-----------------------------------------------------------------
*/

// 杭电discussion 中出现的AC代码,试了一下,确实果断过
int main()
{
int n, i, k;
char input[10000], ch;

scanf("%d", &n);
while (n--)
{
scanf("%s", input);

for (i = 0; i < strlen(input); ++i)
{
k = 0;
ch = input[i];
while (++i < strlen(input) && input[i] == ch)	++k;
--i;
if (k == 0)
printf("%c", ch);
else
printf("%d%c", k+1, ch);
}
printf("/n");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: