#C5758. Longest Common Suffix

    ID: 49442 Type: Default 1000ms 256MiB

Longest Common Suffix

Longest Common Suffix

Given a list of strings, find the longest common suffix among them. The longest common suffix is the longest string that is a suffix of all the given strings. If there is no common suffix, output an empty string.

Note: A suffix of a string is a substring that occurs at the end of the string. For example, the suffixes of "bringing" include "ing", "ng", and "g". In this problem, you are required to compare the end of each string and determine the longest matching sequence.

Examples:

  • For strings ["sprining", "spreading", "bringing"], the longest common suffix is "ing".
  • For strings ["apple", "banana", "orange"], there is no common suffix. Hence, the answer is an empty string.
  • For strings ["cat", "bat", "rat"], the longest common suffix is "at".

inputFormat

The input is read from stdin and is formatted as follows:

  1. The first line contains an integer n which indicates the number of strings.
  2. The following n lines each contain one string.

If n is 0, there are no strings and the output should be an empty string.

outputFormat

The output is written to stdout and is a single line containing the longest common suffix of the input strings. If there is no common suffix, output an empty string.

## sample
3
sprining
spreading
bringing
ing