0
点赞
收藏
分享

微信扫一扫

按字符个数进行编码的ACM基础题的MFC实现

林肯公园_97cc 2022-02-14 阅读 19

该问题的英文描述如下;

Problem Description

Given a stringcontaining 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 linecontains an integer N (1 <= N <= 100) which indicates the number of testcases. The next N lines contain N strings. Each string consists of only 'A' -'Z' and the length is less than 10000.

Output

For each testcase, output the encoded string in a line.

例如,输入如下两行,
ABC
ABBCCC

则输出为
ABC
A2B3C

控制台C++程序和输出如下;

#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
 
//	const int MAXN=10005;
char a[10005];
int main()
{
    int i,temp;
    int T;
    scanf("%d",&T);//两组测试数据 
    while(T--)
    {
        scanf("%s",&a);//输入的是字符串 
        i=0;
        while(a[i]!='\0')//这是判断字符串结束的标志 
        {
            temp=i;
            while(a[temp+1]==a[i])
            {
                temp++;
            }    
            if(temp>i)
			printf("%d",temp-i+1);
            printf("%c",a[i]);
            i=temp;
            i++;
        } 
        printf("\n");   
    } 
    return 0;   
}

 

VC++2012新建一个对话框工程;

void CcencDlg::OnBnClickedButton1()
{
		// TODO: 在此添加控件通知处理程序代码
		//char a[100];
		CString strc, stre;
		int i,temp;
		CString str1;

		GetDlgItem(IDC_EDIT1)->GetWindowTextW(strc);
		i=0;
        while(i<strc.GetLength()) 
        {
            temp=i;
            while(strc.GetAt(temp+1)==strc.GetAt(i))
            {
                temp++;
            }    
            if(temp>i)
			{
				//printf("%d",temp-i+1);
				str1.Format(_T("%d"),temp-i+1);
				stre.Append(str1);
			}
            //printf("%c",a[i]);
			stre.Append((CString)strc.GetAt(i));
            i=temp;
            i++;
        }
		SetDlgItemText(IDC_EDIT2,stre);
}

 运行情况;

 

 

 

 

举报

相关推荐

0 条评论