map存儲所有輸入,vector存儲有用的輸入,然后根據(jù)要求進行倒置即可
#include<iostream>#include<map>#include<string>#include<vector>#PRagma warning(disable:4996)using namespace std;struct node{ int data; string add;//開始表示后一位置的地址,最好表示當前位置的地址};map<string, node> all;//所有的輸入vector<node> rr;//有用的輸入string first;//開始節(jié)點int N, M;int main(){ cin >> first >> N >> M; for (int t = 0;t < N;t++)//map保存所有輸入 { string a; node b; cin >> a >> b.data >> b.add; all[a] = b; } while (first!="-1")//存儲有用的輸入到容器 { node temp; temp.add= first; temp.data = all[first].data; first = all[first].add; rr.push_back(temp); } N = rr.size(); for (int t = 0;M*(t + 1) - 1 < N;t++)//進行翻轉(zhuǎn) { int low = M*t, high = M*(t + 1) - 1; while (low < high) { node temp; temp.add = rr[low].add;temp.data = rr[low].data; rr[low].add = rr[high].add;rr[low].data = rr[high].data; rr[high].data = temp.data;rr[high].add = temp.add; low++; high--; } } int flag = 1; for (auto x : rr)//進行輸出 if (flag == 1) { cout << x.add << " " << x.data << " ";flag = 0; } else cout << x.add << endl << x.add << " " << x.data << " "; cout << -1 << endl;}新聞熱點
疑難解答