Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created September 19, 2016 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/0aa7aedd4c001080128620437f21a59e to your computer and use it in GitHub Desktop.
Save jianminchen/0aa7aedd4c001080128620437f21a59e to your computer and use it in GitHub Desktop.
HackerRank - The hidden message - study code - using DP solution
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
/// <summary>
/// https://www.hackerrank.com/contests/stryker-codesprint/challenges/the-hidden-message
/// </summary>
class Solution3
{
static int LCS(string x, string y)
{
int[,] L = new int[x.Length + 1, y.Length + 1];
for (int i = 0; i <= x.Length; i++)
{
for (int j = 0; j <= y.Length; j++)
{
if (i == 0 || j == 0)
L[i, j] = 0;
else if (x[i - 1] == y[j - 1])
L[i, j] = L[i - 1, j - 1] + 1;
else
L[i, j] = Math.Max(L[i - 1, j], L[i, j - 1]);
}
}
return L[x.Length, y.Length];
}
static void Main(String[] args)
{
TextReader tIn = Console.In;
TextWriter tOut = Console.Out;
string T = tIn.ReadLine();
string phrase = tIn.ReadLine();
string[] P = phrase.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
int N = P.Length;
int[] xP = new int[N];
int fP = 0;
while (fP < N)
{
xP[fP] = T.IndexOf(P[fP], fP == 0 ? 0 : xP[fP - 1] + 1);
if (xP[fP] == -1) break;
fP++;
}
tOut.WriteLine(fP == N ? "YES" : "NO");
tOut.WriteLine(fP > 0 ? string.Join(" ", Enumerable.Range(0, fP).Select(p => string.Format("{0} {1} {2}", P[p], xP[p], xP[p] + P[p].Length - 1)).ToArray()) : "0");
if (fP == N)
{
int lcs = LCS(T, phrase);
tOut.WriteLine(T.Length + phrase.Length - lcs * 2);
}
else
tOut.WriteLine(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment