electicode
HomeCoursesResourcesProblemsNational OlympiadContestsLeaderboard
...

Beautiful name

Time Limit: 1000msMemory Limit: 256MB
View Submissions

Problem Description

Fayoza says that some letters in the alphabet are more beautiful than others. Also, she claims that some names are more beautiful than others.

You are given a set of beautiful letters. The beauty of a name equals the count of beautiful letters in the name. Among a list of nnn names, find the most beautiful one. If there are ties, print the one that appears first in the list.

Input Format

The first line contains an integer nnn --- the number of names.

The second line contains a string --- the set of beautiful letters. All letters are lowercase English letters.

Each of the next nnn lines contains a string --- the names. All names consist of lowercase English letters.

Constraints:

1≤n≤10 0001 \leq n \leq 10\,0001≤n≤10000

∣t∣≤26|t| \leq 26∣t∣≤26, where ttt is the set of beautiful letters.

sL≤2⋅105sL \leq 2 \cdot 10^5sL≤2⋅105, where sLsLsL is the total length of all names.

Output Format

On a single line, print the most beautiful name. If there are ties, print the one that appears earlier in the list.

Scoring

SubtaskConstraintPoints
111t=t =t= "a"353535
222No additional constraints6565

Notes

In the first sample, the beautiful letters are "a", "f", and "s". The beauties are: "malika" =2= 2=2, "fayoza" =3= 3=3, "gulrux" =0= 0=0, "asila" =3= 3=3. Both "fayoza" and "asila" have beauty 333, but "fayoza" appears first, so the answer is "fayoza".

In the second sample, both "hello" and "world" have beauty 000.

Examples

Example 1
Input
4
afs
malika
fayoza
gulrux
asila
Output
fayoza
Example 2
Input
2
a
hello
world
Output
hello

© 2026 Electicode. All rights reserved.

65