python program for following Lexicographical order

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write python program for following

Lexicographical order

Assume that we have alphabet A and the list of words written in this alphabet. Sort this words by lexicographical order consistent with the alphabet A.

Let S1 and S2 will be the words written in A and k will be the number of the first character which differ S1 and S2. S1 is lexicographicaly smaller than S2 if the k-th character in S1 occures earlier than the k-th character in S2 in A or the k-th character from S1, is the last one in this word.

 

Input

In the first line is given the number of tests t. For each test, in separated lines, are written characters from alphabet A which consists of some small letters of the Latin alphabet. Succession of letters defines order of the alphabet. The next line contains the number n (n <100000) - the number of words to sort. Words are placed in the next n lines. Each word consists of only letters from defined alphabet and its length not exceed 1000 letters. You can assume that the total number of letters in the words of each test does not exceed 4e6 (4 000 000). Test cases are separated by empty line.

 

Output

For each test case print sorted words in succeeding lines. After each test case add empty line.

 

Example

Input:

2

re

3

ere

rer

re

 

balujemy

5

bel

luba

lej

bal

leje

 

Output:

re

rer

ere

 

bal

bel

luba

lej

leje

number of the first character which differ S, and S. S, is
lexicographicaly smaller than Sz if the k-th characte hide
oCcures earlier than the k-th character in S, in A or tne x-in
character from S, is the last one in this word.
Input
In the first line is given the number of tests t. For each test,
in separated lines, are written characters from alphabet
A which consists of some small letters of the Latin
alphabet. Succession of letters defines order of the
alphabet. The next line contains the numbr n (n < 100000) -
the number of words to sort. Words are placed in the next
n lines. Each word consists of only letters from defined
alphabet and its length not exceed 1000 letters. You can
assume that the total number of letters in the words of
each test does not exceed 4e6 (4 000 000). Test cases are
separated by empty line.
Output
For each test case print sorted words in succeeding
lines. After each test case add empty line.
Transcribed Image Text:number of the first character which differ S, and S. S, is lexicographicaly smaller than Sz if the k-th characte hide oCcures earlier than the k-th character in S, in A or tne x-in character from S, is the last one in this word. Input In the first line is given the number of tests t. For each test, in separated lines, are written characters from alphabet A which consists of some small letters of the Latin alphabet. Succession of letters defines order of the alphabet. The next line contains the numbr n (n < 100000) - the number of words to sort. Words are placed in the next n lines. Each word consists of only letters from defined alphabet and its length not exceed 1000 letters. You can assume that the total number of letters in the words of each test does not exceed 4e6 (4 000 000). Test cases are separated by empty line. Output For each test case print sorted words in succeeding lines. After each test case add empty line.
Input:
2.
re
ere
rer
re
balujemy
bel
luba
lej
bal
leje
Output:
re
rer
ere
bal
bel
luba
lej
leje
Transcribed Image Text:Input: 2. re ere rer re balujemy bel luba lej bal leje Output: re rer ere bal bel luba lej leje
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Fibonacci algorithm
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education