Vocabulary#5089

Vocabulary (Editoryal)

Insert all the words into a set of strings and then print them in lexicographical order.

Algorithm implementation

Declare a set of strings .

set<string> s;

Read the number of words .

cin >> n;

Read the words and add them to the set .

for (i = 0; i < n; i++)
{
  cin >> str;
  s.insert(str);
}

Print the words in lexicographical order.

for (string str : s)
  cout << str << endl;

Java implementation

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();
  }
}