找位置

题目描述:

对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12 输出:a,1;a,4;a,5;a,10,b,2;b,11,1,8;1,12, 2,9;2,13

输入描述:

输入包括一个由字母和数字组成的字符串,其长度不超过100。

输出描述:

可能有多组测试数据,对于每组数据,
按照样例输出的格式将字符出现的位置标出。

1、下标从0开始。
2、相同的字母在一行表示出其出现过的位置

代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct base
{
    int position;
    struct base* next;
}Base;
class node
{
public:
    char cha;
    int len;
    Base *child,*rear;
    node *next;
    node()
    {
        len=0;
        rear=child=NULL;
        next=NULL;
    }   

    void addPosition(int position)
    {
        if(rear)
        {
            rear->next=new Base;
            rear=rear->next;
            rear->position=position;
            rear->next=NULL;
        }
        else
        {
            //rear=child=(Base*)malloc(sizeof(Base));//见注1
            rear=child=new Base;

            rear->position=position;
            rear->next=NULL;
        }
        len++;
    }

    void Prin()
    {
        if(len>1)
        {
            for(Base*i=child;i;i=i->next)
            {
                if(i==child)
                {
                    printf("%c:%d",cha,i->position);
                }
                else
                {
                    printf(",%c:%d",cha,i->position);
                }
            }
            printf("\n");
        }
    }
};

void f(char *s)
{
    int len=strlen(s);
    node *head=NULL;

    for(int i=0;i<len;i++)
    {
        if(head==NULL)
        {
            head=new node;
            head->cha=s[i];
            head->addPosition(i);
            head->next=NULL;
        }
        else
        {
            node *p=head;

            int flag=1;//避免以下重复插入,不易理解 

            while(p&&p->cha!=s[i])
            {
                if(p->next!=NULL)
                {
                    p=p->next;
                }
                else
                {
                    p->next=new node;
                    p=p->next;
                    p->cha=s[i];
                    p->addPosition(i);
                    p->next=NULL;
                    flag=0; 
                    break;
                }
            }
            if(flag&&p->cha==s[i])
            {
                p->addPosition(i);
            }
        }
    }
    while(head)
    {
        head->Prin();
        head=head->next;
    }
}


int main()
{
    char s[102];
    while(scanf("%s",&s)!=EOF)
    {
        f(s);
    }
    return 0;
}

注:

1.malloc与new的区别:非内部类型来说, malloc不能满足要求的, 因为malloc只是分配堆内存(不会调用构造函数), 而new是分配且内存且在此创建一个对象(会调用构造函数);
2.使用面向对象思想,代码较长但逻辑更清晰,其次笔者先前打算使用一个类似map的实现,但与题目输出不符合,但思想方法不错,留下残缺代码仅供参考。

(写完下面一段,我的dev-cpp崩溃了,完全不能用了,借鉴思想,就这样吧)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int position(int old)//字符转成位置 
{
    if(old>='0'&&old<='9')
    {
        return old-'0';
    }
    else if(old>='a'&&old<='z')
    {
        return old-'a'+10;
    }
    else if(old>='A'&&old<='Z')
    {
        return old-'A'+36;
    }
}
 int rposition(int position)位置反过来转成字符  
 {
    if(position>=0&&position<=9)
    {
        return position+'0';
    }
    else if(position>=10&&position<=35)
    {
        return position+'a'-10;
    }
    else if(position>=36&&position<=61)
    {
        return position+'A'-36;
    }
 }

typedef struct base
{
    int position;
    struct base* next;
}Base;
class node
{
public:
    int len;
    Base * next,*rear;

    node()
    {
        len=0;
        rear=next=NULL;
    }   

    void addPosition(int position)
    {
        if(rear)
        {
            rear->next=(Base*)malloc(sizeof(Base));
            rear=rear->next;
            rear->position=position;
        }
        else
        {
            rear=next=(Base*)malloc(sizeof(Base));
            rear->position=position;
        }
        len++;
    }

    void Prin(int cha)
    {
        if(len>1)
        {
            char t=rposition(cha);
            for(Base*i=next;i;i=i->next)
            {
                if(i==next)
                {
                    printf("%c:%d",t,i->position);
                }
                else
                {
                    printf(",%c:%d",t,i->position);
                }
            }
            printf("\n");
        }
    }
};

void f(char *s)
{
    int len=strlen(s);
    node c[62];
    for(int i=0;i<len;i++)
    {
        c[position(s[i])].addPosition(i);
    }

    for(int i=0;i<62;i++)
    {
        c[i].Prin(i);
    }
}


int main()
{
    char s[102];
    while(scanf("%s",&s)!=EOF)
    {
        f(s);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值