I've Been Everywhere, Man#7546

I've Been Everywhere, Man (Editorial)

Let’s add the names of the cities from each test case to a set of strings. The number of visited cities will be equal to the size of this set.

Algorithm implementation

Declare a string and a set of strings .

string word;
set<string> s;

Read the number of test cases and process each of the tests sequentially.

cin >> tests;
while(tests--)
{

Read the number of cities . Clear the set .

  cin >> n;
  s.clear();

Read the city names and add them to the set .

  while(n--)
  {
    cin >> word;
    s.insert(word);
  }

Print the number of distinct visited cities.

  cout << s.size() << 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 tests = con.nextInt();
    for(int i = 0; i < tests; i++)
    {
      int n = con.nextInt();
      s.clear();
      for(int j = 0; j < n; j++)
      {
        String word = con.next();
        s.add(word);
      }
      System.out.println(s.size());      
    }
    con.close();
  }
} 

Python implementation

Read the number of test cases and process each of the tests sequentially.

tests = int(input())
for _ in range(tests):

Read the number of cities . Clear the set .

  n = int(input())
  s = set({})

Read the city names and add them to the set .

  for _ in range(n):
    s.add(input())

Print the number of distinct visited cities.

  print(len(s))