0% found this document useful (0 votes)
38 views4 pages

The String Puzzle

Uploaded by

ariyanpandey1998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

The String Puzzle

Uploaded by

ariyanpandey1998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

The String Puzzle

Problem Description
Ajantha, a book lover who enjoys exploring books, discovered an ancient book one
day. Inside, she found an interesting puzzle. The puzzle featured a tree-like
structure where each node is a word in an unknown language. Instead of the full
hierarchy, she was only given the edges between the words. Additionally, she was
provided with another string containing words from the hierarchy and asked to
determine the value of this string. The value is calculated by adding the levels of
all the words within the string to which they belong, starting from level 1.

Consider the edges are unidirectional from first node to second node. Also note
that if a word is not present in the given hierarchy, its level will be -1.

Given the edges between the words and the string as input, calculate and print the
value of the string.

Constraints
1 <= number of edges <= 100

1 <= number of words in the input string <= 1000

The words will be having only lowercase alphabets.

Input
First line consists of an integer N, denoting the number of edges.

Next N lines consist of two space separated words, denoting that there exists an
edge between them.

Last line consists of a string for which you need to calculate the value.

Output
Single integer denoting the value of the given input string.

Time Limit (secs)


1

Examples
Example 1

Input

abcd wxyz

vsmoh bhwbb

crpyc ynkna

bhwbb uhhjm

crpyc eokuo

ynkna abcd

bhwbb uvjyu

vsmoh crpyc
bhwbb enzwb

uhhjm wxyz vsmoh crpyc bhwbb

Output

13

Explanation

Upon processing the input words the following tree-like hierarchy of words will
emerge.

com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@7f4d9395:image1.png

Hence the value of the given string will be 3 (uhhjm) + 5 (wxyz) + 1 (vsmoh) + 2
(crpyc) + 2 (bhwbb) = 13. Hence, print the 13 as the output.

Example 2

Input

abc pqr

xyz mno

abc uvw

mno abc

qwe xyz

mno xyz pqr mno pqr uvw abc qwe

Output

28

Explanation

Upon processing the input words the following tree-like hierarchy of words will
emerge.

com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@7f4d9395:image2.png

Hence the value of the given string will be 3 (mno) + 2 (xyz) + 5 (pqr) + 3 (mno) +
5 (pqr) + 5 (uvw) + 4 (abc) + 1 (qwe) = 28. Hence, print 28 as output.

ANSWER:
using System;
using System.Collections.Generic;

class HierarchyProcessor
{
static void Main()
{
int edgeCount = int.Parse(Console.ReadLine());
var adjacencyMap = new Dictionary<string, List<string>>();
var incomingEdgeCount = new Dictionary<string, int>();

for (int i = 0; i < edgeCount; i++)


{
var relation = Console.ReadLine().Split();
string source = relation[0];
string destination = relation[1];

if (!adjacencyMap.ContainsKey(source))
adjacencyMap[source] = new List<string>();
adjacencyMap[source].Add(destination);

if (!incomingEdgeCount.ContainsKey(source))
incomingEdgeCount[source] = 0;
if (!incomingEdgeCount.ContainsKey(destination))
incomingEdgeCount[destination] = 0;
incomingEdgeCount[destination]++;
}

var targetWords = Console.ReadLine().Split();

var nodeLevels = new Dictionary<string, int>();


var bfsQueue = new Queue<string>();

foreach (var node in incomingEdgeCount.Keys)


{
if (incomingEdgeCount[node] == 0)
{
nodeLevels[node] = 1; // Root level is 1
bfsQueue.Enqueue(node);
}
}

while (bfsQueue.Count > 0)


{
var currentNode = bfsQueue.Dequeue();
int currentLevel = nodeLevels[currentNode];

if (adjacencyMap.ContainsKey(currentNode))
{
foreach (var neighbor in adjacencyMap[currentNode])
{
if (!nodeLevels.ContainsKey(neighbor))
{
nodeLevels[neighbor] = currentLevel + 1;
bfsQueue.Enqueue(neighbor);
}
incomingEdgeCount[neighbor]--;
if (incomingEdgeCount[neighbor] == 0 && !
nodeLevels.ContainsKey(neighbor))
{
bfsQueue.Enqueue(neighbor);
}
}
}
}
int cumulativeValue = 0;

foreach (var word in targetWords)


{
if (nodeLevels.ContainsKey(word))
{
cumulativeValue += nodeLevels[word];
}
else
{
cumulativeValue += -1;
}
}

Console.Write(cumulativeValue);
}
}

You might also like