题目详情

首先给出两个节点的地址和数值N,两个地址分别是两个单词的首字母,N是全部节点的数量。随后,以 地址 数值 下一节点的地址 的方式逐行输入N个节点。要求输出两个字母相同后缀的起始地址,如果两个字母没有相同的后缀,则输出-1。

我的版本

在做这道题的时候,发现在Idea能够正常编译的代码到Pat中运行结果为空,未查明原因。

需要注意一个问题:相同value的index不一定相同,例如a在不同word里面的index可能就不同,此时认为具有两个index的a不等同。在我的代码中,使用index+value的方式记录具有不同index的同一value。

  1. import java.util.*;
  2. import java.io.*;
  3. public class Main {
  4. static class Node {
  5. String val;
  6. Node next;
  7. }
  8. public static void main(String[] args) throws IOException{
  9. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  10. String[] line = br.readLine().split(" ");
  11. String i1 = line[0];
  12. String i2 = line[1];
  13. int n = Integer.parseInt(line[2]);
  14. HashMap<String, Node> hm = new HashMap<>();
  15. HashMap<String, String> rs = new HashMap<>();
  16. Node n1, n2;
  17. for (int i = 0; i < n; i ++) {
  18. line = br.readLine().split(" ");
  19. n1 = hm.computeIfAbsent(line[0], y -> new Node());
  20. n1.val = line[0] + line[1];
  21. n2 = hm.computeIfAbsent(line[2], y -> new Node());
  22. n1.next = n2;
  23. rs.put(n1.val, line[0]);
  24. }
  25. n1 = hm.get(i1);
  26. n2 = hm.get(i2);
  27. String w2 = "";
  28. while (n2 != null) {
  29. w2 += n2.val;
  30. n2 = n2.next;
  31. }
  32. n = w2.length();
  33. while (n1.next != null) {
  34. int ti = w2.indexOf(n1.val);
  35. if (ti != -1 && n > ti) {
  36. n = ti;
  37. n2 = n1;
  38. }
  39. n1 = n1.next;
  40. }
  41. if (n != w2.length()) {
  42. System.out.println(rs.get(n2.val));
  43. } else {
  44. System.out.println(-1);
  45. }
  46. }
  47. }

参考版本

  1. import java.util.Scanner;
  2. public class Main {
  3. /**
  4. * 到牛客网提交,不然会有一组数据超时
  5. *
  6. * */
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. int startA = scanner.nextInt(), startB = scanner.nextInt(), n = scanner.nextInt();
  10. scanner.nextLine();
  11. Node1032[] linked = new Node1032[100000 + 11];
  12. for (int i = 0; i < n; ++i) {
  13. String[] inputStrings = scanner.nextLine().split("\\s+");
  14. int address = Integer.parseInt(inputStrings[0]);
  15. int next = Integer.parseInt(inputStrings[2]);
  16. Node1032 tempNode = new Node1032();
  17. tempNode.address = address;
  18. tempNode.data = inputStrings[1];
  19. tempNode.next = next;
  20. linked[address] = tempNode;
  21. }
  22. scanner.close();
  23. boolean tag = false;
  24. int resultAddress = -1;
  25. while(startA!=-1) {
  26. linked[startA].flag = true;
  27. startA = linked[startA].next;
  28. }
  29. while(startB!=-1) {
  30. if (linked[startB].flag==true) {
  31. resultAddress = startB;
  32. tag = true;
  33. break;
  34. }
  35. startB = linked[startB].next;
  36. }
  37. if (tag) {
  38. System.out.printf("%05d\n", resultAddress);
  39. } else {
  40. System.out.println(resultAddress);
  41. }
  42. }
  43. }
  44. class Node1032 {
  45. Integer address;
  46. String data;
  47. Integer next;
  48. boolean flag;
  49. }

总结

  • 我使用hashmap寻址,并借助字符串的方法进行检索。参考版本通过新建一个超大的list直接寻址
  • 参考版本给Node节点设置Flag判断是否为重复点。我的方法有点绕,先将word以string方式存储之后使用indexOf方法进行查询。对比之下,参考方法更简洁。
  • 参考版本初始化超大的list,直接占用了至少100011*4byte的内存,真的可取吗?