Encodingless
Time Limit:1000MS Memory Limit:65536K
Total Submit:62 Accepted:35this
Descriptionspa
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.
code
Inputip
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.string
Outputit
For each test case, output the encoded string in a line.io
Sample Inputclass
2
ABC
ABBCCC
Sample Outputtest
ABC
A2B3C
Source
#include <stdio.h> #include <string.h> #define MAXN 10000 + 10 char str[MAXN]; int main() { int n, i, k, c, thisn, maxn; scanf("%d", &n); while (n--) { c = 0; scanf("%s", str); k = strlen(str); str[k] = -1; // 這裏的輸入長度小於定義的長度因此不會越界 thisn = maxn = 0; for ( i=0; i<k; i++ ) { thisn++; if ( thisn > maxn ) { //實時更新段的長度 maxn = thisn; } if ( str[i+1] != str[i] ) { // 當後面的與前面的不一樣時輸出以前的 thisn = 0; if ( maxn == 1 ) { printf("%c", str[i]); } else { printf("%d%c", maxn, str[i]); maxn = 0; // 輸出後將最大長度再次賦值爲0 } } } printf("\n"); } return 0; }