hdu 1020 Encoding
Posted wz-archer
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
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <cmath> #include <queue> #include <deque> #include <cmath> #include <map> using namespace std; typedef long long ll; const double inf=1e20; const int maxn=1e5+10; const int mod=1e9+7; char a[maxn]; char s[maxn]; int num[maxn]; int main(){ int t; scanf("%d",&t); while(t--){ scanf("%s",a); int len=strlen(a); int j=0; s[0]=a[0]; num[0]=1; for(int i=1;i<len;i++){ if(a[i]==s[j])num[j]++; else { s[++j]=a[i]; num[j]=1; } } for(int i=0;i<=j;i++){ if(num[i]!=1){ printf("%d",num[i]); } printf("%c",s[i]); }printf(" "); } return 0; }
以上是关于hdu 1020 Encoding的主要内容,如果未能解决你的问题,请参考以下文章