Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address
is the position of the node, Data
is an integer, and Next
is the position of the next node.
Output Specification:
For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
Sample Output:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
---------------------------------------这是题目和解题的分割线---------------------------------------
和这道题的思路差不多吧PAT 1097 Deduplication on a Linked List,但是我脑子大概是进浆糊了,最后两个测试点卡了好久,最后发现一个是没看清题目,是反转链表,即按照id倒序输出而不是按照data倒序= = 还有一个是反转的次数是有效结点/K,而不是N/K(明明注意到这一点但傻了忘了这个地方也要改 = = )链表(静态)的题都要记得小心无效结点
大概思路是先按照单链表的顺序排序,再按照k分组排序。
#include<cstdio>
#include<algorithm>
const int maxN = 100010;
using namespace std;
struct node
{
int data;
int address; //当前地址
int next;
int id; //链表次序
}list[maxN];
bool cmp1(node a,node b)
{
return a.id<b.id;
}
bool cmp2(node a,node b)
{
return a.id>b.id;
}
int main()
{
int begin,n,k,i,j;
scanf("%d%d%d",&begin,&n,&k);
int address;
for(i=0;i<n;i++)
{
scanf("%d",&address);
scanf("%d%d",&list[address].data,&list[address].next);
list[address].address = address;
}
//初始化成超大的数,以便排序
for(i=0;i<maxN;i++)
list[i].id = 2*maxN;
int p = begin,order = 0;
//遍历,并标上序号
while(p!=-1)
{
list[p].id = order++;
p = list[p].next;
}
//按单链表的顺序排序
sort(list,list+maxN,cmp1);
//是order/k不是n/k 存在无效结点干扰的情况
int turn = order/k;
for(i=0,j=0;j<turn;i=i+k,j++)
sort(list+i,list+i+k,cmp2);
for(i=0;i<order-1;i++)
printf("%05d %d %05d\n",list[i].address,list[i].data,list[i+1].address);
printf("%05d %d -1\n",list[order-1].address,list[order-1].data);
return 0;
}
换了个方法试试,如下:
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
int address;
int data;
int next;
int id;
}list[100010],print[100010];
bool cmp(node a,node b)
{
return a.id>b.id;
}
int main()
{
int begin,n,k,i,cnt = 0;
scanf("%d%d%d",&begin,&n,&k);
for(i=0;i<n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
list[a].address = a;
list[a].data = b;
list[a].next = c;
}
//将正确顺序存在另一个结构体中
while(begin!=-1)
{
print[cnt++] = list[begin];
print[cnt-1].id = cnt-1;
begin = list[begin].next;
}
int t = 0;
//需要reverse时也可以通过设置id,再排序来实现
//注意排序和输出都是cnt不是n,因为题目存在无效结点
for(i=0;i<cnt/k;i++)
{
sort(print+t*k,print+t*k+k,cmp);
t++;
}
for(i=0;i<cnt;i++)
{
printf("%05d %d",print[i].address,print[i].data);
if(i!=cnt-1) printf(" %05d\n",print[i+1].address);
else printf(" -1\n");
}
return 0;
}