編碼
Time Limit: 1000MS Memory limit: 65536K
題目描述
給你一個由大寫字母組成的組成的字符串,你可以用如下規則對其進行編碼:
1、 包含K個相同字母的連續字符串可以用KX表示,其中X是相同的字母。
2、 如果K爲1,不輸出K
輸入
輸入有多組,直到文件結束。每組一個字符串,長度爲10000以內
輸出
輸出編碼後的字符串。
示例輸入
示例輸出
ABC
A2B3C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char data;
struct node *next;
};
int main()
{
char a[10000];
int i,k,t[10000];
while(gets(a)!=NULL)
{
struct node *head,*p,*tail,*q,*r;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
k=strlen(a);
for(i=0;i<k;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;
p->data=a[i];
tail->next=p;
tail=p;
}
p=head;
i=0;
while(p)
{
q=p;
t[i]=1;
while(q->next)
{
if(q->next->data==p->data)
{
t[i]++;
r=q->next;
q->next=r->next;
free(r);
}
else break;
}
p=p->next;
i++;
}
p=head->next;
i=1;
while(p)
{
if(t[i]!=1)
printf("%d",t[i]);
i++;
printf("%c",p->data);
p=p->next;
}
printf("\n");
q=head;
while(head->next!=NULL)
{
r=q->next;
q->next=r->next;
free(r);
}
head->next=NULL;
}
return 0;
}