题目解析
给定一个链表 求出他 绝对值没有重复的链表 和 被删除的链表
解题思路
单链表的运用
现将存链表 然后遍历他 看值的绝对值是否有出现过
有出现过 就 丢到 L 中
没出现过 就 丢到 shan 中
然后按格式输出
当前节点 值 下一个节点
...
当前节点 值 -1
注: 我java 过不去 TLE
代码
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
static int N = (int) 1e5;
static int shu[] = new int[N + 10];
static int ne[] = new int[N + 10];
static boolean vis[] = new boolean[N + 10];
public static void main(String[] args)
{
int start = sc.nextInt();
int n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
shu[a] = b;
ne[a] = c;
}
ArrayList<Integer> ar = new ArrayList<Integer>();
ArrayList<Integer> shan = new ArrayList<Integer>();
for (int i = start; i != -1; i = ne[i])
{
int x = Math.abs(shu[i]);
if (vis[x])
shan.add(i);
else
{
ar.add(i);
vis[x] = true;
}
}
for (int i = 0; i < ar.size(); i++)
{
int x = ar.get(i);
if (i == ar.size() - 1)
out.printf("%05d %d %d\n", x, shu[x], -1);
else
out.printf("%05d %d %05d\n", x, shu[x], ar.get(i + 1));
}
for (int i = 0; i < shan.size(); i++)
{
int x = shan.get(i);
if (i == shan.size() - 1)
out.printf("%05d %d %d\n", x, shu[x], -1);
else
out.printf("%05d %d %05d\n", x, shu[x], shan.get(i + 1));
}
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}
c++
#include <cstdio>
#include <cmath>
#include <vector>
using namespace std;
const int N = 1e5;
int shu[N + 10], ne[N + 10];
bool vis[N + 10];
int start, n;
int a, c, b;
int main()
{
scanf("%d%d", &start, &n);
for(int i = 1; i <= n; i ++)
{
scanf("%d%d%d", &a, &c, &b);
shu[a] = c;
ne[a] = b;
}
vector<int> L, shan;
for(int i = start; i != -1; i = ne[i])
{
int x = abs(shu[i]);
if(vis[x])
shan.push_back(i);
else
{
vis[x] = true;
L.push_back(i);
}
}
for(int i = 0; i < L.size(); i ++)
{
int x = L[i];
if(i == L.size() - 1)
printf("%05d %d %d\n", x, shu[x], -1);
else
printf("%05d %d %05d\n", x, shu[x], L[i + 1]);
}
for(int i = 0; i < shan.size(); i ++)
{
int x = shan[i];
if(i == shan.size() - 1)
printf("%05d %d %d\n", x, shu[x], -1);
else
printf("%05d %d %05d\n", x, shu[x], shan[i + 1]);
}
return 0;
}