Peter decided to
compile an explanatory dictionary of the Orcish language. He approached each of
the n orcs in turn; each of them
pronounced one word in Orcish, and Peter wrote it down. He chose not to include
definitions for the words – after all, every orc already knows their meaning.
Now all the work that remains for you to do (Peter himself has already grown
tired of this) is to sort the resulting list of words in lexicographical order.
Input. The first line contains the number of words n (1 ≤ n
≤ 100).
The following n lines contain
the words of the Orcish language, consisting only of uppercase letters.
The length of each word does not exceed 100 characters.
Output. Print Peter’s
dictionary – n Orcish words sorted in
lexicographical order.
|
Sample input |
Sample
output |
|
3 AB A AA |
A AA AB |
data structures – set
Insert all the
words into a set of strings and then print them in lexicographical order.
Declare a set of strings s.
set<string> s;
Read the number of words n.
cin >> n;
Read the words and add them to the set s.
for (i = 0; i < n; i++)
{
cin >> str;
s.insert(str);
}
Print the words in lexicographical order.
for (string str : s)
cout << str << endl;
#include <stdio.h>
#include <string.h>
#include <malloc.h>
char s[100][101];
char str[101];
int i, j, n, len;
int main(void)
{
scanf("%d\n", &n);
for (i = 0; i < n; i++)
{
gets_s(str);
strcpy(s[i], str);
}
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++)
if (strcmp(s[i],
s[j]) > 0)
{
strcpy(str, s[i]);
strcpy(s[i], s[j]);
strcpy(s[j], str);
}
for (i = 0; i < n; i++)
puts(s[i]);
return 0;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> s;
string str;
int i, j, n, len;
int main(void)
{
cin >> n;
for (i = 0; i < n; i++)
{
cin >> str;
s.push_back(str);
}
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++)
if (s[i] > s[j])
{
str = s[i];
s[i] = s[j];
s[j] = str;
}
for (i = 0; i < n; i++)
cout << s[i] << endl;
return 0;
}
import java.util.*;
public class Main
{
public static void main(String []args)
{
Scanner con = new Scanner(System.in);
TreeSet<String> s = new TreeSet<String>();
int n = con.nextInt();
for(int i = 0; i < n; i++)
s.add(con.next());
for(String st : s)
System.out.println(st);
con.close();
}
}