#include<bits/stdc++.h>
using namespace std;
struct lian{
string address,next;
long long data;
}s1[100001],s2[100001];
int main(){
string str;
int n,k;
cin>>str>>n>>k;
int i,l;
int j=0;
for(i=0;i<n;i++)cin>>s1[i].address>>s1[i].data>>s1[i].next;
for(i=0;i<n;i++){
if(s1[i].address==str){
s2[j].address=s1[i].address;s2[j].data=s1[i].data;s2[j].next=s1[i].next;break;
}
}
while(s2[j].next!="-1"){
for(i=0;i<n;i++){
if(s1[i].address==s2[j].next){
j++;s2[j].address=s1[i].address;s2[j].data=s1[i].data;s2[j].next=s1[i].next;break;
}
}
}
j++;
for(i=k-1;i<j-j%k;i+=k){
if(i==k-1)cout<<s2[i].address<<' '<<s2[i].data;
else cout<<' '<<s2[i].address<<"\n"<<s2[i].address<<' '<<s2[i].data;
for(l=1;l<k;l++){
cout<<' '<<s2[i-l].address<<"\n"<<s2[i-l].address<<' '<<s2[i-l].data;
}
}
if(j<k){
for(i=0;i<j;i++){
cout<<s2[i].address<<' '<<s2[i].data<<' '<<s2[i].next<<endl;
}
}
else {
for(i=j-j%k;i<j;i++){
cout<<' '<<s2[i].address<<"\n"<<s2[i].address<<' '<<s2[i].data;
}
cout<<" -1"<<endl;
}
}
请教一下大佬为什么第23行加上break后会超时,去掉后反而正确了?
全部评论
(0) 回帖