1 solutions

  • 0
    @ 2026-10-22 18:20:38

    C :

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void getAllRear(char** rearArr, char* str, int strLen) {
        for (int i = 0; i < strLen; i++) {
            rearArr[i] = str + i;
        }
    }
    
    
    void swap(char** a, char** b) {
        char* tmp = *a;
        *a = *b;
        *b = tmp;
    }
    
    
    void sort(char* rearArr[], int len) {
        for (int i = 0; i < len - 1; i++) {
            for (int j = 0; j < len - i - 1; j++) {
                if (strcmp(rearArr[j], rearArr[j + 1]) > 0) {
                    swap(&rearArr[j], &rearArr[j + 1]);
                }
            }
        }
    }
    
    int main() {
        char* str = (char*)malloc(1000001);
        char** rearArr = (char**)malloc(1000001);
        scanf("%s", str);
        int len = strlen(str);
    
        getAllRear(rearArr, str, len);
    
        sort(rearArr, len);
    
        for (int i = 0; i < len; i++) {
            int pos = rearArr[i] - str + 1;
            printf("%d ", pos);
        }
        printf("\n");
    
        return 0;
    }
    

    C++ :

    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    bool compareSuffixes(const string& text, int pos1, int pos2) {
        return text.substr(pos1) < text.substr(pos2);
    }
    
    int main() 
    {
        string inp;
        cin >> inp;
        int length = inp.length();
        vector<int> po(length);
    
        for (int i = 0; i < length; ++i) 
    	{
            po[i] = i;
        }
        sort(po.begin(), po.end(), [&](int a, int b) 
    	{
            return compareSuffixes(inp, a, b);
        });
    
        for (int i = 0; i < length; ++i) 
    	{
            cout << po[i] + 1 << " ";
        }
        cout << endl;
        return 0;
    }
    

    Java :

    import java.util.*;
    
    public class Main {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String s = scanner.nextLine();
            int n = s.length();
    
            List<Suffix> suffixes = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                suffixes.add(new Suffix(i, s.substring(i)));
            }
    
    
            Collections.sort(suffixes);
    
    
            for (Suffix suffix : suffixes) {
                System.out.print((suffix.i + 1) + " ");
            }
        }
    
    
        static class Suffix implements Comparable<Suffix> {
            int i;
            String s;
    
            public Suffix(int i, String s) {
                this.i = i;
                this.s = s;
            }
    
            @Override
            public int compareTo(Suffix other) {
                return this.s.compareTo(other.s);
            }
        }
    }
    

    Python :

    # coding=utf-8
    # 读取输入字符串
    s = input()
    n = len(s)
    
    # 生成所有后缀
    suffixes = []
    for i in range(n):
        suffixes.append((s[i:], i+1))  # 存储后缀和原始位置
    
    # 按字典序排序后缀
    suffixes.sort()
    
    # 输出排序后的位置
    sa = [pos for _, pos in suffixes]
    print(*sa)
    
    • 1

    Information

    ID
    349
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    (None)
    # Submissions
    0
    Accepted
    0
    Uploaded By