编码(链表)

来源:互联网 发布:java war包反编译 编辑:程序博客网 时间:2024/05/21 09:25

编码

Time Limit: 1000MS Memory limit: 65536K

题目描述

给你一个由大写字母组成的组成的字符串,你可以用如下规则对其进行编码:
1、 包含K个相同字母的连续字符串可以用KX表示,其中X是相同的字母。
2、 如果K为1,不输出K

输入

 输入有多组,直到文件结束。每组一个字符串,长度为10000以内

输出

 输出编码后的字符串。

示例输入

ABCABBCCC

示例输出

ABCA2B3C
#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;}
0 0
原创粉丝点击