# Huawei online programming question series-14-string connection longest path search

Problem Description:

## 1. The problem involves knowledge points

• string type sort
• Repeat
• Priority queue

## 2. Solve by yourself

• Solution 1: directly compare the size of characters according to the idea of sorting, or use string.compareTo for comparison
```package com.chaoxiong.niuke.huawei;

import java.util.Scanner;
/**
* Create by tianchaoxiong on 18-4-9.
* // The dictionary sort of strings can be sorted directly
*/
public class HuaWei_14 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
String[] stringArr = new String[N];
int stringArrIndex = 0;
for (int i = 0; i < N; i++) {
inSert(scanner.next(), stringArr, stringArrIndex);
stringArrIndex++;
}
//        Arrays.sort(stringArr);
for (String each : stringArr)
System.out.println(each);

}

private static void inSert(String key, String[] stringArr, int stringArrIndex) {
if(stringArrIndex==0){
// If it's a direct insert
stringArr[stringArrIndex]=key;
}else {
// Insert by insertion sort
int i;
for(i=stringArrIndex-1;i>=0;i--){
if(isSmall(key,stringArr[i])){
//move
stringArr[i+1] = stringArr[i];
}else
break;
}
stringArr[i+1] = key;

}
}
private static boolean isSmall(String a, String b) {
// Returns true if dictionary sequence a is less than b
char []aArr = a.toCharArray();
char []bArr = b.toCharArray();
int index = 0;
while (index<aArr.length&&index<bArr.length){
if(aArr[index]<bArr[index]){
return true;
}else {
if(aArr[index]>bArr[index]){
return false;
}else {
index++;
}
}
}
return aArr.length == index;
}
}
```
• Solution 2: store the data in a priority queue. Traverse the output
```package com.chaoxiong.niuke.huawei;

import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
/**
* Create by tianchaoxiong on 18-4-9.
*/
public class HuaWei_14_2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
scanner.nextLine();
Queue<String>priorityQueue = new PriorityQueue<String>(N);
for (int i = 0; i < N; i++) {
}
while (priorityQueue.peek()!=null)
System.out.println(priorityQueue.poll());
}
}```

• Use ArrayList to store data
• Use Collections.sort(arrayList) to sort the data
```import java.util.*;
public class Main{
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
ArrayList<String> set=new ArrayList<String>();
int num=scan.hasNextLine()?Integer.parseInt(scan.nextLine()):0;
while(--num>=0&&scan.hasNextLine()){
}
Collections.sort(set);
for(String str:set){
System.out.println(str);
}
}
}```

## 4. Summary of this question

string.compareTo:

```// 123.compareTo("123") return 0;
// 123.compareTo("1234") return -1;
// 123.compareTo("12345") return -2;
// 1234.compareTo("12") return 2;
// 1234.compareTo("1237") return -3;
// 1234.compareTo("12375") return -3;
// 12375.compareTo("1233") return 4;```

arrayList can directly put basic types of data, and then sort them by means of Collections.sort(obj)

Tags: Java less

Posted on Thu, 19 Mar 2020 14:25:03 -0400 by antonbrk