首页 > 试题广场 >

字符串排序

[编程题]字符串排序
  • 热度指数:521989 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定 n 个字符串,请对 n 个字符串按照字典序排列。

数据范围: ,字符串长度满足

输入描述:
输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。


输出描述:
数据输出n行,输出结果为按照字典序排列的字符串。
示例1

输入

9
cap
to
cat
card
two
too
up
boat
boot

输出

boat
boot
cap
card
cat
to
too
two
up
我用
while (in.hasNextLine()) { // 注意 while 处理多个 case
            String b = in.nextLine();
            if(b != null && b != "") {
                map.put(b,0);      
            }               
}
剔除掉空字符串,结果神奇的来了:调试显示是正确的,第一行不是空;自测运行第一行却是空的。
编辑于 2024-04-25 09:34:19 回复(0)
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = Integer.valueOf(in.nextLine());
        ArrayList<String> arr = new ArrayList<>();
        for(int i=0;i<num;i++) {
            arr.add(in.nextLine());
        }

        Collections.sort(arr);
        for(String str:arr) {
            System.out.println(str);
        }
    }
}

发表于 2024-04-24 10:39:40 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String str = sc.next();
            list.add(str);
        }
        Collections.sort(list);
        for (String s : list) {
            System.out.println(s);
        }
    }
}
编辑于 2024-04-15 19:56:33 回复(0)
这么做应该没问题吧
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        TreeMap<String, String> map = new TreeMap<String, String>();
        int i = Integer.parseInt( in.nextLine());
        while (in.hasNextLine()) {
            String str = in.nextLine();
            String value = String.valueOf(str);
            if (str.isEmpty()) {
                break;
            }
            //用treeMap 自动排序,让key和value 相同插入,
            //因为不能去掉重复的值,所以在有重复key出现时拼接1;
            while (map.containsKey(str)) {
                str = str + "1";
            }
            if (!map.containsKey(str)) {
                map.put(str, value);
            }
        }
        for (Map.Entry<String, String> o : map.entrySet() ) {
            System.out.println(o.getValue());
        }
    }
}
发表于 2024-04-06 12:26:19 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<String> r = new ArrayList();
        while (n -- > 0) {
            r.add(in.next());
        }
        Collections.sort(r);
        r.forEach(System.out::println);
    }
}

编辑于 2024-03-16 16:55:20 回复(0)
暴力解法。。。。。
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            String [] str = new String[a];
            for (int i = 0; i < a; i++) {
                str[i] = in.next();
            }
            // 使用选择排序
            for (int i = 0; i < a; i++) {
                int minIndex = i;
                for (int j = i + 1; j < a; j++) {
                    if (!isSwamp(str[minIndex], str[j])) {
                        minIndex = j;
                    };
                }
                if (minIndex != i) {
                    String temp = str[i];
                    str[i] = str[minIndex];
                    str[minIndex] = temp;
                }
            }
            for (String str1 : str) {
                System.out.println(str1);
            }
        }
    }

    public static boolean isSwamp(String word1, String word2) {
        int strLength = Math.min(word1.length(), word2.length());
        for (int i = 0; i < strLength; i++) {
            // 字符一比较出大小,就结束循环
            if (word1.charAt(i) < word2.charAt(i)) {
                return true;
            } 
            if (word2.charAt(i) < word1.charAt(i)){  
                return false;                          
            }
        }
        // 遍历部分字符相同,则比较长度,长度较大的必定排在后面
        return word1.length() > word2.length()? false:true;
    }
}

发表于 2024-03-15 06:21:23 回复(0)
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] arr = new String[n];
        for (int i = 0; i < n; i++) {
            arr[i] = in.next();
        }
        Arrays.sort(arr);
        for (String s : arr) {
            System.out.println(s);
        }
    }
}

编辑于 2024-03-06 19:40:26 回复(1)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<String> list =new ArrayList<String>();
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            int a = new Integer(s);
            for(int i=0;i<a;i++){
                String tmp=in.nextLine();
                list.add(tmp);
            }
            Collections.sort(list);
            for(String i:list){
                System.out.println(i);
            }
        }
    }
}
发表于 2023-11-30 16:03:31 回复(0)
import java.util.Scanner;

public class test14 {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner sc=new Scanner(System.in);
        int num=0;
        
        if(sc.hasNextInt())
        {
             num=sc.nextInt();

        }
        StringBuffer str[]=new StringBuffer[num];
        for(int i=0;i<num;i++)
        {
            str[i]=new StringBuffer();
            if(sc.hasNext())
            str[i]=str[i].append(sc.next());
            
        }
        for(int i=0;i<str.length;i++)
        {
            for(int j=i+1;j<str.length;j++)
            {
                StringBuffer change=null;
                for(int k=0;k<Math.min(str[i].length(),str[j].length() );k++)
                {
                    if(str[i].charAt(k)>str[j].charAt(k))
                    {
                        change=str[i];
                        str[i]=str[j];
                        str[j]=change;
                        break;
                    }
                    else if(str[i].charAt(k)<str[j].charAt(k))
                    {
                        break;
                    }
                    else
                    {
                         if(k==Math.min(str[i].length(),str[j].length() )-1)
                        {
                            if(str[i].length()>str[j].length()) {
                                change=str[i];
                                str[i]=str[j];
                                str[j]=change;
                                break;
                            }
                            else
                            {
                                break;
                            }
                        }
                         else {
                             continue;
                         }
                    }
                    
                }
            }
        }
        
        for(int i=0;i<str.length;i++)
        {
            System.out.println(str[i]);
        }
        sc.close();

    }

}
暴力手搓,感觉这时间和体积差点炸了。
发表于 2023-10-30 16:41:31 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        while (in.hasNextInt()) {
            int n = in.nextInt();
            String[] alls = new String[n];
            for (int i = 0; i < n; i++) {
                alls[i] = in.next();
            }
            quickSort(alls, 0, n - 1);
            for (String all : alls) {
                System.out.println(all);
            }
        }
    }

    private static void quickSort(String[] alls, int l, int r) {
        if (l >= r) return;

        int mid = l + r >> 1, i = l - 1, j = r + 1;
        String x = alls[mid];

        while (i < j) {
            while (compare(x, alls[++i]) > 0) ;
            while (compare(x, alls[--j]) < 0) ;

            if (i < j) {
                String tmp = alls[i];
                alls[i] = alls[j];
                alls[j] = tmp;
            }
        }
        quickSort(alls, l, j);
        quickSort(alls, j + 1, r);
    }

    public static int compare(String s1, String s2) {
        int l1 = s1.length();
        int l2 = s2.length();

        for (int i = 0; i < l1 && i < l2; i++) {
            if (s1.charAt(i) > s2.charAt(i))
                return 1;
            if (s1.charAt(i) < s2.charAt(i))
                return -1;
        }
        return Integer.compare(l1, l2);
    }
}
快排

发表于 2023-10-24 20:58:18 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int size = in.nextInt();
        String[] words = new String[size];
        for(int i = 0; i < words.length; i++) {
            words[i] = in.next();
        }
        Arrays.sort(words);
        for (String word : words) {
            System.out.println(word);
        }
    }
}

这种题目的意义是什么?考察是否知道有sort这个函数??
发表于 2023-08-14 18:29:48 回复(1)
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Map;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int total = Integer.parseInt(in.nextLine());
        TreeMap<String, Integer> tm = new TreeMap<>();

        for (int i = 0; i < total; i++) {
            String key = in.nextLine();
            if (tm.containsKey(key)) {
                tm.put(key, tm.get(key) + 1);
            } else {
                tm.put(key, 1);
            }
        }
        for (Map.Entry<String, Integer> entry : tm.entrySet()) {
            for (Integer integer = 0; integer < entry.getValue(); integer++) {
                System.out.println(entry.getKey());
            }
        }

    }
}

发表于 2023-08-09 00:02:06 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int num = in.nextInt();
        List<String> list = new ArrayList<>();
        while(in.hasNext()){
            list.add(in.next());    
        }
        Collections.sort(list);
        for(String s : list){
            System.out.println(s);
        }
    }
}

发表于 2023-07-09 13:04:05 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n=in.nextInt();
        List <String> list=new ArrayList<>();
        for(int i=0;i<n;i++)
        {
            String str=in.next();//nextLine会接受前面回车空格
            list.add(str);
        }
        Collections.sort(list);
        for(String str:list)
        {
            System.out.println(str);
        }
    }
}
发表于 2023-06-23 00:13:09 回复(0)

一开始用的是TreeSet,发现会丢失重复数据,后来换成了TreeMap。

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String, Integer> tm = new TreeMap<String, Integer>();

        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            for (int i = 0; i < a; i++) {
                String s = in.next();
                if (tm.containsKey(s)) {
                    tm.put(s, tm.get(s) + 1);
                } else {
                    tm.put(s, 1);
                }
            }
        }
        tm.forEach((key, value)-> {
            for (int i = 0; i < value; i++) {
                System.out.println(key);
            }
        });
    }
}
发表于 2023-06-16 16:37:24 回复(0)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String first = in.next();
        ArrayList<String> list = new ArrayList<>();
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            list.add(in.next());
        }
	  	// 创建一个String类型的比较器.
        Comparator<String> com=Comparator.naturalOrder();
	  	// 通过比较器对list中的数据进行排序
        list.sort(com);
        for (String str : list) {
            System.out.println(str);
        }
    }
}


发表于 2023-06-02 16:13:08 回复(0)

直接使用Collections.sort(list)排序就行了,默认是升序

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            int length = input.nextInt();
            int i = 0;
            List<String> list = new ArrayList<>();
            while (i < length) {
                list.add(input.next());
                i++;
            }
            Collections.sort(list);
            list.forEach(System.out::println);
        }
    }
}
发表于 2023-05-15 22:25:16 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class Main {
    public static void main(String[] args) throws IOException {
        
    useArraysSort();

    }
//方法一使用数组排序
static void useArraysSort(){

 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String [] str =     new String[Integer.parseInt( reader.readLine())];
            String n = "";
            for (int i = 0; (n = reader.readLine()) != null; i++) {
                str[i] = n;
            }
            Arrays.sort(str);
            for (String s : str) {
                System.out.println(s);
            }
}
    //方法二使用优先队列
   static void usePriorityQueue(){
       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        PriorityQueue<String> priorityQueue =     new PriorityQueue<>(Integer.parseInt( reader.readLine()));
        ;
        String n = "";
        while ((n = reader.readLine()) != null) {       
                priorityQueue.offer(n);
        }
        while (!priorityQueue.isEmpty()) {
            System.out.println(priorityQueue.poll());
        }
   }

}



发表于 2023-05-10 22:54:53 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int totalNum = scanner.nextInt();
        String[] arr = new String[totalNum];
        for(int i=0;i<totalNum;i++)
        {
            arr[i] = scanner.next();
        }
        for(int i = 0;i<totalNum-1;i++)
            for(int j = 0;j<totalNum-i-1;j++)
            if(arr[j].compareTo(arr[j+1])>0)
            {
                String temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        }
    }
发表于 2023-04-27 00:44:38 回复(0)