1097. Deduplication on a Linked List (25)(段错误)

//  main.cpp
//  Dedeplication_on_a_Linked_List
//
//  Created by ZDQ on 16/6/25.
//  Copyright © 2016年 ZDQ. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include <map>
#include<cstring>
#define MAX 1000001
using namespace std;
typedef struct node{
    int address;
    int key;
    char nt[11];
}Node;
map<string,Node> input;//输入的所有数据
string first[MAX];//第一次出现的key的集合
string second[MAX];//第二次
int cluster[MAX];//已经出现的key的集合
int main()
{
    int num;
    char  start[10];
    Node nd;
    for(int i=0;i<MAX;i++)
        cluster[i]=MAX;
    scanf("%s%d",start,&num);
    int tem=num;
    while(tem--)
    {
        int k;
        char a[10],n[10];
        scanf("%s%d%s",a,&k,n);
        nd.key=k;
        strcpy(nd.nt,n);
        input[a]=nd;
    }
   // while(1); //运行超时
    nd=input[start];
    first[0]=start;
    int i=0,j=0;
    cluster[abs(nd.key)]=1;
    char next[10];
    strcpy(next,start);
    //while(1);运行超时
    while(strcmp("-1",next)!=0)
    {
        strcpy(next,nd.nt);
        nd=input[next];
        while(cluster[abs(nd.key)]==1)// repeat continue;
        {
            second[j]=next;
            j++;
            strcpy(next,nd.nt);
            if(strcmp("-1",next)==0)
                break;
            nd=input[next];
         //   if(j>=MAX)
            //    while(1); // 运行超时 段错误
            //j 段错误
        }
        i++;
        cluster[abs(nd.key)]=1;
       // if(i>=MAX)
         //   while(1); 段错误
        first[i]=next;
    }
   // while(1); 段错误 运行超时
    for(int ii=0;ii<i-1;ii++)
            printf("%s %d %s\n",first[ii].c_str(),input[first[ii]].key,first[ii+1].c_str());
    printf("%s %d %d\n",first[i-1].c_str(),input[first[i-1]].key,-1);
    //while(1);段错误 运行超时
    //if(j>num)
     //   while(1); 段错误 段错误
    for(int ii=0;ii<j-1;ii++)
        printf("%s %d %s\n",second[ii].c_str(),input[second[ii]].key,second[ii+1].c_str());
   //while(1);段错误 运行超时
    printf("%s %d %d\n",second[j-1].c_str(),input[second[j-1]].key,-1);// 段错误
    //while(1); 段错误 段错误

    return 0;
}

该代码已在牛课网上通过,但是在PAT上出现段错误,错误位置已初步锁定,但是不知道为何。请大神明示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值