Algorithm
Problem Name: beecrowd | 2826
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2826
Lexical
By Emilio Wuerges, UFFS Brazil
Timelimit: 1
As we know, the lexicon is the set of words in a language. In western languages, it is usual to write using the Latin alphabet, with 26 letters from a to z.
It is usual to enumerate the Latin letters in the following order: a, b, c, d, e f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z.
If a list of words is sorted using this order, it is much easier to find them. Your job in this problem is to sort 2 words using this ordering.
Given 2 words A and B. If the first character of A comes before the first character of B, place A before B. If it is the same character, we use the following character to break the tie. If the second is also the same, we use the following, an so on. When all characters from A are equal to the beginning of B, or if all characters of B are equal to the beginning of A, we place the shorter word first.
Input
The input contains 2 words using lowercase characters from a to z. The length of the words is not larger than 20 characters.
Output
The output contains the same 2 words in lexicographical order.
Input Samples | Output Samples |
abc |
abc |
bcd |
abc |
abcd |
abc |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
int main (void)
{
char palavra1[25], palavra2[25];
unsigned short i, j, tamanho1, tamanho2;
scanf("%s %s", palavra1, palavra2);
if (strcmp(palavra1, palavra2) > 0)
{
printf("%s\n", palavra2);
printf("%s\n", palavra1);
}
else if (strcmp(palavra1, palavra2) < 0)
{
printf("%s\n", palavra1);
printf("%s\n", palavra2);
}
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
string s1, s2;
vector < string>v;
cin >> s1 >> s2;
v.push_back(s1);
v.push_back(s2);
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); ++i)
cout << v[i] << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner leitor = new Scanner(System.in);
String A = leitor.next();
String B = leitor.next();
if (A.compareTo(B) < 0) {
System.out.println(A);
System.out.println(B);
} else if (A.compareTo(B) > 0) {
System.out.println(B);
System.out.println(A);
} else {
System.out.println(A);
System.out.println(B);
}
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("node:fs")
const input = readFileSync("/dev/stdin", "utf8")
.toLowerCase()
.split("\n", 2)
function main() {
const output = input.sort((wordA, wordB) => {
return wordA.localeCompare(wordB, "en-US")
})
console.log(output.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with Python Programming
Code -
Python Programming
a = input()
b = input()
l = []
l.append(a)
l.append(b)
l.sort()
for x in l:
print(x)
Copy The Code &
Try With Live Editor
Input
Output