Peter decided to
create a dictionary of Orc language. He came to each of n Orcs, and each Orc told him one word, and Peter wrote this word.
Peter decided not to write the definitions for these words because every Orc
knows their meaning. So, all you have left (Peter is already tired of this
idea) is to sort the list of words in lexicographic order.
Input. The first line contains the number of words n (1 ≤ n
≤ 100). The next n lines contain
the words of the Orc language, consisting of only uppercase Latin letters. The
length of the words is no more than 100.
Output. Print the resulting Peter’s
vocabulary – n Orc words in
lexicographic order.
Sample input |
Sample
output |
3 AB A AA |
A AA AB |
data structures – set
Put all the words
in a set of strings. Then print them in lexicographic order.
Declare a set s
of strings and an iterator to it.
set<string>
s;
set<string>::iterator
iter;
Read the number of words n.
scanf("%d\n",&n);
Read the words and insert them into the set s.
for(i = 0; i < n; i++)
{
gets(str);
s.insert(str);
}
Print the words in lexicographic order.
for(iter = s.begin(); iter != s.end();
iter++)
puts((*iter).c_str());
set<string> s;
Read the number of words n.
cin >> n;
Read the words and insert
them into the set s.
for (i = 0; i < n; i++)
{
cin >> str;
s.insert(str);
}
Print the words
in lexicographic 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();
}
}