Deep beneath the Grand Library of Arcanis, an apprentice mage discovers a sealed vault containing a collection of enchanted scrolls. Each scroll bears a long sequence of characters, but the true message is hidden within.
The text on each scroll is structured as follows: meaningful spell fragments are separated by a recurring magic glyph pattern. To decode the scroll, you must:
Important: When the glyph pattern appears consecutively (with nothing between two occurrences), or at the very beginning or end of the text, no empty fragment is produced — simply skip over them. Only non-empty fragments are kept.
Within each spell fragment, rearrange the characters according to the following rules (applied in order of priority):
A–Z) come before lowercase letters (a–z), which come before digits (0–9).A < B < ... < Z; a < b < ... < z).0 < 1 < ... < 9).String: MagicXScrollXAA11bbba Glyph pattern: X
Step 1 — Split: Splitting by X yields: ["Magic", "Scroll", "AA11bbba"]
Step 2 — Sort each fragment:
| Fragment | Sorted | Explanation |
|---|---|---|
Magic |
Macgi |
All appear once. Uppercase M first, then lowercase a, c, g, i in dictionary order. |
Scroll |
llScor |
l appears twice. Among once-appearing: uppercase S, then lowercase c, o, r. |
AA11bbba |
bbbAA11a |
b appears 3 times, A and 1 appear twice, a appears once. Priority: Frequency > Type (Upper > Lower > Digit) > Order. |
char *strstr(const char *haystack, const char *needle);
needle in the null-terminated string haystack.NULL if the substring is not found.void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
nmemb elements of size size.base: Pointer to the first element of the array to be sorted.nmemb: Number of elements in the array.size: Size in bytes of each element in the array.compar: A pointer to a function that compares two elements. It should return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.alphabet[] Priority Array
main.c: A-Z (indices 0-25), a-z (indices 26-51), 0-9 (indices 52-61).function.h:
extern char alphabet[65]; //implement split string function, return 2d char array to store result, set correct number of splitted strings char **split(char* string, const char* pattern, int* splittedStrings); //free memory space void free_(char **result, int splittedStrings); //sort each splitted string void sort( char* string);
main.c:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "function.h" #define MAX_S_LEN 505 #define MAX_P_LEN 15 char alphabet[65]; /** * Main function for the Enchanted Scroll Decoder. * This part of the code is handled by the server and cannot be modified by students. */ int main() { char s[MAX_S_LEN], p[MAX_P_LEN]; scanf("%s %s", s, p); // Initialize the global alphabet priority array: A-Z, a-z, 0-9 char *ptr = alphabet; for (char c = 'A'; c <= 'Z'; c++) *ptr++ = c; for (char c = 'a'; c <= 'z'; c++) *ptr++ = c; for (char c = '0'; c <= '9'; c++) *ptr++ = c; *ptr = '\0'; int fragment_count = 0; char **fragments = split(s, p, &fragment_count); for (int i = 0; i < fragment_count; i++) { sort(fragments[i]); } free_(fragments, fragment_count); return 0; }
Output each sorted spell fragment on its own line, in the order they appear in $S$. The sort function in your function.c must handle the printing of the sorted fragment followed by a newline.