HDU 1020 Encoding 字符统计
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1020 Encoding 字符统计相关的知识,希望对你有一定的参考价值。
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
Sample Output
ABC
A2B3C
水题之一,就是给你n串字符,按顺序输出字母连续出现的次数。
表示一开始将数字和字母位置弄反了,导致一直WA的我真是无药可救了。
代码如下:
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 using namespace std; 6 char ch[15000],before; 7 int num,n; 8 int main(){ 9 scanf("%d",&n); 10 while(n!=0) 11 { 12 n--; 13 scanf("%s",ch); 14 int len=strlen(ch); 15 before=ch[0]; 16 num=1; 17 for (int i=1;i<len;++i) 18 { 19 if (ch[i]==before) 20 num++; 21 else 22 { 23 if (num!=1) 24 printf("%d%c",num,before); 25 else 26 printf("%c",before); 27 num=1; 28 before=ch[i]; 29 } 30 31 }if (num!=1) 32 printf("%d%c\n",num,before); 33 else 34 printf("%c\n",before); 35 36 } 37 return 0; 38 }
以上是关于HDU 1020 Encoding 字符统计的主要内容,如果未能解决你的问题,请参考以下文章