[Bridge Cup Blue] suffix sorting substring

Problem Description

Description
For a string, the substring suffix sorting, for example, grain
which has substring:
grain
Rain
AIN
in
n-
then lexicographically sorting strings, i.e., for each sub-:
AIN, grain, in, n-, Rain
Input
plurality of sets of inputs, each input line string.
Output
Substring sort output, each line a string
Sample Input
Grain
Banana
Sample Output
AIN
Grain
in
n-
Rain
A
ANA
anana
Banana
Na
Nana

Thinking problems

  1. Each string constructed first, and then sorted
  • Be careful not to reference #include <algorithm>or memory will explode
  • A plurality of sets of input uncertainty, should be used while(cin >> i)i can be a number, or a string

Specific code

#include <iostream>
// 不要轻易引用这个内存会爆炸的
// #include <algorithm>
using namespace std;

int main()
{
    string i;
    int head=0;
    while(cin>>i){
        string data[1000];
        head = 0;
        for(int k=0;k<i.length();k++){
            data[head++] = i.substr(head,i.length());
        }
        for(int k=head;k>=0;k--){
            for(int k2=head;k2>0;k2--){
                if(data[k2]>data[k2-1]){
                    string temp = data[k2];
                    data[k2] = data[k2-1];
                    data[k2-1] = temp;
                }
            }
        }
        for(int k=head-1;k>=0;k--){
            cout << data[k] << endl;
        }
    }
    return 0;
}
Published 31 original articles · won praise 13 · views 9873

Guess you like

Origin blog.csdn.net/qq_43497702/article/details/104046311