// 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上出现段错误,错误位置已初步锁定,但是不知道为何。请大神明示。

&spm=1001.2101.3001.5002&articleId=51863754&d=1&t=3&u=d3ac808dd744420ca3fe6a11aacc67a1)
229

被折叠的 条评论
为什么被折叠?



