Description
The strings are sorted from small to large in ASCII code order.
The ASCII code sequence for a string follows the following recursive definitions:
12 strings of the first n-1 characters are the same, the nth character ASCII code sequence is small in front;
2 only two strings of characters are identical, only two strings are equal.
The ASCII code order comparison of the characters can be done with the strcmp () function.
Input
The first line is entered as an integer n (n<=50,000), followed by n rows, one string per line, and the string length does not exceed 100,000.
Output
The output is n lines, followed by the ASCII code sequence of the string, and the ASCII code sequence is small in front of the row.
Sample Input10abcbcacacacaacababcdabasample Outputaacabaabcacababcbcccadahint
It is difficult to allocate such a large amount of space with a two-dimensional array, and the dynamically allocated memory based on the input changes is used. There is a need for a dynamic data structure, such as an array of character pointers "Char *s[]", or a two-dimensional character pointer "char **s", and so on.
Append Code
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 Chars[100000+Ten];5 intcmpConst void*a,Const void*b) {6 Char* S1=* ((Char**) a);7 Char* S2=* ((Char**) b);8 if(strcmp (S1,S2) >0)return 1;Else return 0;9 }Ten intMain () { One intN; Ascanf"%d",&N); - gets (s); - Char* * strs= (Char**)malloc(sizeof(Char*)*N); the for(intI=0; i<n;i++){ - gets (s); -Strs[i]= (Char*)malloc(sizeof(Char) * (strlen (s) +5)); - strcpy (strs[i],s); + } -Qsort (Strs,n,sizeof(Char*), CMP); + for(intI=0; i<n;i++){ Aprintf"%s\n", Strs[i]); at Free(Strs[i]); - } - Free(STRs); - return 0; -}
Sdust OJ problem G dynamic string ordering