using
System;
using
System.Collections.Generic;
public
class
GFG
{
public
static
void
LongestGoodSequence(List<Pair<
string
,
string
>> arr, List<
int
>[] adj, List<
int
> ind)
{
Queue<
int
> q =
new
Queue<
int
>();
for
(
int
i = 0; i < 26; i++)
{
if
(ind[i] == 0)
{
q.Enqueue(i);
}
}
List<
char
> ans =
new
List<
char
>();
long
maxi = 1;
while
(q.Count > 0)
{
List<
char
> ans1 =
new
List<
char
>();
int
for1 = q.Dequeue();
ans1.Add((
char
)(for1 +
'a'
));
while
(adj[for1].Count > 0)
{
ans1.Add((
char
)(adj[for1][0] +
'a'
));
for1 = adj[for1][0];
}
if
(ans1.Count > maxi)
{
maxi = ans1.Count;
ans =
new
List<
char
>(ans1);
}
}
foreach
(
char
c
in
ans)
{
Console.Write(c +
" "
);
}
}
public
static
void
Main()
{
List<Pair<
string
,
string
>> arr =
new
List<Pair<
string
,
string
>>
{
new
Pair<
string
,
string
>(
"ab"
,
"ac"
),
new
Pair<
string
,
string
>(
"yz"
,
"xz"
),
new
Pair<
string
,
string
>(
"ee"
,
"ee"
),
new
Pair<
string
,
string
>(
"aaac"
,
"abag"
),
new
Pair<
string
,
string
>(
"g"
,
"h"
)
};
List<
int
>[] adj =
new
List<
int
>[26];
for
(
int
i = 0; i < 26; i++)
{
adj[i] =
new
List<
int
>();
}
List<
int
> ind =
new
List<
int
>(
new
int
[26]);
foreach
(
var
pair
in
arr)
{
int
len = pair.First.Length;
for
(
int
j = 0; j < len; j++)
{
if
(pair.First[j] != pair.Second[j])
{
int
val1 = pair.First[j] -
'a'
;
int
val2 = pair.Second[j] -
'a'
;
ind[val2]++;
adj[val1].Add(val2);
}
}
}
LongestGoodSequence(arr, adj, ind);
}
public
class
Pair<K, V>
{
private
K first;
private
V second;
public
Pair(K first, V second)
{
this
.first = first;
this
.second = second;
}
public
K First {
get
{
return
first; } }
public
V Second {
get
{
return
second; } }
}
}