Sdust OJ problem G dynamic string ordering

Source: Internet
Author: User
Tags strcmp

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.