PTA linked list de duplication

Keywords: C++ data structure linked list

56 line less than AC

Idea: (look here! Look here!
[original] it is troublesome to delete duplicate elements (move them to another array at the same time) and change the address of the data behind the original table. It is troublesome and time complexity is high.
[now] one key point is that the next address of the data is not required.

  1. Change to sequential storage in the table. When outputting directly, you can output the address of the next one.
  2. It is realized by two-dimensional array, [0] is the address of the current data and [1] is the current data value.
  3. Use v [data value] to find out whether the value already exists. If it does not exist, move it to the left two-dimensional array; if it exists, move it to the two-dimensional array where the data is deleted.

Given a linked list L with integer key values, you need to delete the key value nodes with duplicate absolute values, that is, for each key value K, only the first absolute value is equal to K
At the same time, all deleted nodes must be saved in another linked list. For example, given that L is 21 → - 15 → - 15 → - 7 → 15, you need to output the linked list after de duplication
21 → - 15 → - 7, and the deleted linked list - 15 → 15.

Input format:

Enter the address of the first node of L given in the first line and a positive integer N (≤ 105, the total number of nodes). The address of a node is a non negative 5-bit integer, empty address
NULL is represented by - 1. Then there are N lines, and each line describes a node in the following format:

The address key value is the next node, where the address is the address of the node, the key value is an integer with an absolute value of no more than 10 ^ 4, and the next node is the address of the next node.

Output format:

First output the linked list after de duplication, and then output the deleted linked list. Each node occupies a row and is output according to the input format.

Input example:

00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854
(no blank line at the end)

Output example:

00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1
(no blank line at the end)

My AC Code:

#include<bits/stdc++.h>
#include<string.h>
using namespace std;

struct {
    int data;
    int next;
}a[100012];

int main(){
    int n,address,num,i,j,k,p;
    cin>>address>>n;
    int a_add[n];  //Originally ordered address 
    int number[n];//It is used to store the original data
	bool v[10005]={0};//It is used to traverse whether the data is repeated
	int left_add[n][2];//Used to store numbers with the same absolute value
	int remain_add[n][2];//Data left behind 
	
    for(i=0;i<n;i++){
    	int da,ne;
		cin>>num>>da>>ne;//Fill in address, data and next node
		a[num].data=da;
		a[num].next=ne;
	}
    for(address,j=0;address!=-1;j++){
        a_add[j]=address;
		number[j]=a[address].data;//Sequential storage
        address=a[address].next;
    }
    i=0,k=0,p=0;
    //----------------------------------------------------
	while(i<j) {
		if(v[abs(number[i])]){//It's been pure 
			left_add[k][0]=a_add[i];//Storage address 		
			left_add[k][1]=number[i];//Item 2 stored data 
			k++;
		}else{
			remain_add[p][0]=a_add[i];//Storage address 		
			remain_add[p][1]=number[i];//Item 2 stored data
			p++;
			v[abs(number[i])]=1;
		}
		i++;
	}
	//Output de duplication first and then linked list
	for(int i=0;i<p;i++){
		if(i==p-1)printf("%05d %d -1\n",remain_add[i][0],remain_add[i][1]);
		else printf("%05d %d %05d\n",remain_add[i][0],remain_add[i][1],remain_add[i+1][0]);
	}
	//Then output the deleted linked list 
	for(int i=0;i<k;i++){
		if(i==k-1)printf("%05d %d -1",left_add[i][0],left_add[i][1]);
		else printf("%05d %d %05d\n",left_add[i][0],left_add[i][1],left_add[i+1][0]);
	}
    return 0;
}

Code that ran timeout before: (although I don't think anyone will see it, I still put it up)

#include<bits/stdc++.h>
#include<string.h>
using namespace std;

struct {
    int data;
    int next;
}a[100012];

int main(){
    int n,address,num,i,j,k=0;
    cin>>address>>n;
    int a_add[n];  //Is an address enough orz no 
    int number[n];//Is used to store data
	int left_add[n][2];//Used to store numbers with the same absolute value
 
    for(i=0;i<n;i++){
    	int da,ne;
		cin>>num>>da>>ne;//Fill in address, data and next node
		a[num].data=da;
		a[num].next=ne;
	}
    for(address,j=0;address!=-1;j++){
        a_add[j]=address;
		number[j]=a[address].data;//Sequential storage
        address=a[address].next;
    }
    i=0,k=0;
    //----------------------------------------------------
	while(i<j) {
		int flag=0;
		//The number at this time is the first address of a_add[i]
		for(int m=0;m<i;m++){
			if(number[m]==number[i]||number[m]+number[i]==0){
				flag=1;
				break;
			}
		}//Check whether there are duplicates, if any, directly come out
		if(flag){
			//Transfer to deleted table
			left_add[k][0]=a_add[i];//Storage address 
			left_add[k][1]=number[i];//Item 2 stored data 
			a[a_add[i-1]].next=a[a_add[i]].next;
			//Delete a_add [i]
			for(int p=i;p<j-1;p++){
				a_add[p]=a_add[p+1];
				number[p]=number[p+1];
			} 
			j--;
			k++;
			continue;
		}
		i++;
 }
	//Output de duplication first and then linked list
	for(int i=0;i<j;i++){
		if(i==j-1)printf("%05d %d -1\n",a_add[i],a[a_add[i]].data);
		else printf("%05d %d %05d\n",a_add[i],a[a_add[i]].data,a_add[i+1]);
	}
	//Then output the deleted linked list 
	for(int i=0;i<k;i++){
		if(i==k-1)printf("%05d %d -1",left_add[i][0],left_add[i][1]);
		else printf("%05d %d %05d\n",left_add[i][0],left_add[i][1],left_add[i+1][0]);
	}
    return 0;
}

Posted by sangamon on Sun, 10 Oct 2021 04:57:23 -0700