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; }
相关文章推荐
- Hoj 1020 Afshung Pizza Delivery
- HOJ 1020
- 1020
- 1020. 月饼 (25)
- HOJ 又见GCD 2.1.5
- PAT乙级 1011-1020
- HOJ 1461 Gene Shuffle
- HDU 2756-HOJ-Unique Snowflakes
- 1020 孪生蜘蛛
- PAT 乙级 1020. 月饼 (25)
- hdu 1020 Encoding
- PAT 1020 Tree Traversals(树重建+BF 4000 S)
- PAT甲级 1020
- XYNU OJ 1020—1029基础题(适合于C语言初学者)
- HDU1020(水题)
- 1020(树状数组,逆序对+离散化)
- (poj 1020 Anniversary Cake)<DFS的巧妙剪枝>
- PAT (Advanced) 1020. Tree Traversals (25)
- 1020. Rope
- PAT甲级1119,1086,1020总结