Given a graph G, its vertices have been numbered from 0 to 9. Some letters have also been assigned to vertices of G, as can be seen from the following picture:
Given a string S. Your task is to determine whether there is a walk W which realizes a given string S in graph G, and if so, find the lexicographically least such walk.
Explanation:
Let’s consider the picture from the statement carefully. The key observation is that there’s no vertex V in G such that it’s connected with two other vertices with the same letter written on them.
In other words, if we are looking for a way to continue the current walk from a vertex V and the next letter in S is C then two options are possible:
V is not connected with vertices labled with C, so it’s impossible to continue the current walk;
V is connected with exactly one vertex U labled with C, so there’s only one way to continue the current walk.
The next observation is that there are only two possible vertices to start our walk. After that observation we are ready to compose the whole solution:
Choosing the starting vertex of the walk;
Trying to complete the walk by moving to the corresponding vertices(if possible);
Finding the lexicographically least valid walk. It’s sufficient to compare paths only by their starting vertices.
There is an interesting fact that the only situation when there are two valid paths is when S consists of one symbol concatenated several times with itself. For example, there are two valid paths 3838 and 8383 for S = DDDD.
I have a dp solution which is not getting submitted (WA). I’ve input many test cases and it is working fine for all of them.
Approach:
Since we know the letters, we just need to know whether it is taken from the inner or the outer ring.
Let 1: outer ring, 2: inner ring
dp1[i] = 2 tells that we are now at outer ring(1) and previous node was in the inner ring(2). Similar is the case of dp2[i].
So considering consecutive letters
if they are same, we are switching levels
dp2[i] = 1;
dp1[i] = 2;
if the modular difference between the two node is 1
we have moved in the outer ring
dp1[i] = 1;
if it is 2
we have moved in the inner ring
dp2[i] = 2;
dpX values are set only if previous nodes are reachable ie dpX[i-1] != -1
Later, the path is calculated by tracing back from dp1[n] and dp2[n] considering the letter at each position.
Order is checked only by the first node. Its giving correct answer for cases like “AAAA”(0505), “ABBECCD”(0169723), “D”(3), “AAB”(501), “AAAB”(0501).
Hi during the testing I created list of basic test cases:
Solution is @Shashwat.delhi’s I just modified it such, that when line starts with ‘#’ I print this line and continue with next - it’s easier to navigate in test cases then…
Hi, I considered as two graphs one as outer and other as inner. There can by only one path after that we can determine after examining 1st three vertices in the string.Can someone please help me to find out where i am missing?
I replaced the picture with the one from problem statement, it seems to me, that origin URL is not reachable… If it was different picture, please fix the link.
I have checked for all corner cases…My solution passed all test cases generated by betlista… Could somebody help me where i am missing? Or provide the test cases used for evaluation?