现在的位置: 首页 > 综合 > 正文

hdu Encoding 1020

2012年08月25日 ⁄ 综合 ⁄ 共 1224字 ⁄ 字号 评论关闭

Encoding

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

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
 1 /* 这题刚开始理解错了,全部统计的,wa了,
2 * 再看题是字串,所以应该是连续的,边处理边输出
3 */
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7
8 char str[11000];
9 int num[11000];
10
11
12 void solve( )
13 {
14 int i, j, len, t;
15
16 len = strlen(str);
17
18 for( i = 0; i < len; i++) {
19 if (str[i] == str[i + 1])
20 num[str[i] - 'A']++;
21 else if ( num[str[i] - 'A'] == 1)
22 printf("%c",str[i]);
23 else if ( num[str[i] - 'A'] > 1) {
24 printf("%d",num[str[i] - 'A']);
25 printf("%c",str[i]);
26 num[str[i] - 'A'] = 1;
27 }
28 }
29 puts("");
30 }
31
32
33
34
35 int main( )
36 {
37 int N, i;
38 scanf("%d", &N);
39 while (N--) {
40 for( i = 0; i < 10100; i++)
41 num[i] = 1;
42 scanf("%s", str);
43 solve( );
44 }
45 return 0;
46 }

抱歉!评论已关闭.