wa了一个点 还在思考
用了离散化的反向操作 将有序映射成无序
需要自己先构造一个有序
int mm = 1;
ys[mm++] = fs[s];
while(s!="-1"){
s=nes[s];
ys[mm++] = fs[s];
}
核心代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
//map<string,int> idx ;
string idx[N],ne[N]; //基本元素 当前id和下一个的id
map<string,int> da ;//用 字符串来匹配 data
map<string,int> fs ;//反身 idx的反身下标数组
map<string,string> nes; //用于找到正确的链表顺序
int ys[N]; //映射数组 保证从有序映射到无序真实
int main()
{
string f ,f2;
int n;
cin>>f>>n; string s = f;
// idx[1] = f;
int id;
for (int i = 1; i <= n; i ++ ){
cin>>f>>id>>f2;
idx[i] = f;
fs[f] = i;
da[f] = id;
ne[i] = f2;
nes[f] = f2;
}
int mm = 1;
ys[mm++] = fs[s];
while(s!="-1"){
s=nes[s];
ys[mm++] = fs[s];
}
int cnt = 1;
for (int i = 1; i <= (n+1)/2; i ++ ){
for(int j = 0;j<2;j++){
if(n%2==1&&i==(n+1)/2) j=1;
if(cnt%2==1){
int j = n-i+1;
cout<<idx[ys[j]]<<" ";
cout<<da[idx[ys[j]]]<<" ";
if(n%2==1&&i==(n+1)/2) cout<<-1<<endl;
else
cout<<idx[ys[i]]<<endl;
}else{
int j = n-i+1;
cout<<idx[ys[i]]<<" ";
cout<<da[idx[ys[i]]]<<" ";
if(n%2==0&&n-i==n/2)
cout<<-1<<endl;
else
cout<<idx[ys[n-i]]<<endl;
}
cnt++;
}
}
//7 1 6 2 5 3 4
}
ac 代码
这个题不能把n直接当做合法链表的总数
有脏数据
所以应该用mm
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
//map<string,int> idx ;
string idx[N],ne[N];
map<string,int> da ;
map<string,int> fs ;
map<string,string> nes;
int ys[N];
int main()
{
string f ,f2;
int n;
cin>>f>>n; string s = f;
// idx[1] = f;
int id;
for (int i = 1; i <= n; i ++ ){
cin>>f>>id>>f2;
idx[i] = f;
fs[f] = i;
da[f] = id;
ne[i] = f2;
nes[f] = f2;
}
int mm = 1;
ys[mm++] = fs[s];
while(s!="-1"){
s=nes[s]; //cout<<s<<endl;
ys[mm++] = fs[s];
}
// cout<<mm-2<<endl;
n = mm-2;
int cnt = 1;
for (int i = 1; i <= (n+1)/2; i ++ ){
for(int j = 0;j<2;j++){
if(n%2==1&&i==(n+1)/2) j=1;
if(cnt%2==1){
int j = n-i+1;
cout<<idx[ys[j]]<<" ";
cout<<da[idx[ys[j]]]<<" ";
if(n%2==1&&i==(n+1)/2) cout<<-1<<endl;
else
cout<<idx[ys[i]]<<endl;
}else{
int j = n-i+1;
cout<<idx[ys[i]]<<" ";
cout<<da[idx[ys[i]]]<<" ";
if(n%2==0&&n-i==n/2)
cout<<-1<<endl;
else
cout<<idx[ys[n-i]]<<endl;
}
cnt++;
}
}
//7 1 6 2 5 3 4
}