Tail sequence Problems

Source: Internet
Author: User

Problem:

In the book "programming Pearl", we mentioned the character string rotation problem. For example, if a character string AB is rotated to obtain the character string Ba, you can get it through (a 'B, "'" indicates that the entire string is rotated.

In this case, for example, "ABACE", you can get "eabac", "ceaba", "aceab", "bacea" by rotating at different positions and sort these strings alphabetically, get:

ABACE

Aceab

Bacea

Ceaba

Eabac

In this way, the tail sequence is "ebaac ". The question is, how can we get the first string sorted by Dictionary Based on the given tail sequence "ebaac?

Analysis:

According to the tail sequence, we can get all the letters in the string, and then sort the letters to get the first letter of all the strings. Due to this nature, "A *** e" through right shift, we can get "EA ***". Therefore, the letter "E" must be followed by "A", which is an important property.

A *** E

A *** B

B ***

C ***

E *** C

But what if there are duplicate letters? For example

B ***

C ***

Then "A" can be followed by "B" and "C". However, we can sort the strings starting with "A" in alphabetical order, it must be followed by a small letter, so the first string starting with "A" must be followed by "B ", the string starting with the second "A" must be followed by "C". Then, determine the second letter. How can we determine the third letter? Similarly, after "B" is determined, we move one digit to the right of the row ending with "B" and get "ba ***", know that the letter after "B" must be "A", so that B is followed by a, and then get the "c" next to this ''a ", and so on.

CodeAs follows:

# Include <stdio. h> # include <stdlib. h> # include <string. h> struct a {int ind; // record subscript, which is very important char ch;}; int comp (const void * X, const void * Y) {return (struct a *) X)-> CH> (struct a *) y)-> CH | (struct a *) X) -> CH = (struct a *) y)-> ch // If the letters are the same, the rows with small subscripts are listed in the front. & (struct a *) X) -> ind> (struct a *) y)-> IND);} int main () {char s [50] = "ebaac", F [50]; int I, j, Len; struct a [50]; scanf ("% s", S); Len = strlen (s); for (I = 0; I <Len; I ++) {A [I]. IND = I; A [I]. ch = s [I];} printf ("Origin: % d: % s \ n", Len, S); qsort (A, Len, sizeof (struct ), comp); // perform the fast forward for (I = 0; I <Len; I ++) {printf ("% d, % C \ n", a [I]. IND, a [I]. ch) ;}for (I = 0, j = 0; I <Len; I ++) {f [I] = A [J]. ch; j = A [J]. IND;} f [Len] = '\ 0'; printf ("% s \ n", f); Return 0 ;}

Note: The subscript is used to save the row information as follows:

2 A *** E 0
3 A *** B 1
1 B *** A 2
4 C *** A 3
0 e *** C 4

Summary:

Pay attention to the nature of the problem analysis, and be good at solving problems by nature.

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.