Soluție HackerRank pentru Yet Another KMP Problem. Include cerința formatată, exemple, explicația pașilor și cod sursă.
- Problemă: Yet Another KMP Problem
Cerinta completa
This challenge uses the famous KMP algorithm. It isn’t really important to understand how KMP works, but you should understand what it calculates.
A KMP algorithm takes a string, [Expresie matematică indisponibilă în copia arhivată], of length [Expresie matematică indisponibilă în copia arhivată] as input. Let’s assume that the characters in [Expresie matematică indisponibilă în copia arhivată] are indexed from [Expresie matematică indisponibilă în copia arhivată] to [Expresie matematică indisponibilă în copia arhivată]; for every prefix of [Expresie matematică indisponibilă în copia arhivată], the algorithm calculates the length of its longest valid border in linear complexity. In other words, for every [Expresie matematică indisponibilă în copia arhivată] (where [Expresie matematică indisponibilă în copia arhivată]) it calculates the largest [Expresie matematică indisponibilă în copia arhivată] (where [Expresie matematică indisponibilă în copia arhivată]) such that for every [Expresie matematică indisponibilă în copia arhivată] (where [Expresie matematică indisponibilă în copia arhivată]) there is [Expresie matematică indisponibilă în copia arhivată].
Here is an implementation example of KMP:
kmp[1] = 0;
for (i = 2; i <= N; i = i + 1){
l = kmp[i - 1];
while (l > 0 && S[i] != S[l + 1]){
l = kmp[l];
}
if (S[i] == S[l + 1]){
kmp[i] = l + 1;
}
else{
kmp[i] = 0;
}
}
Given a sequence [Expresie matematică indisponibilă în copia arhivată], construct a string, [Expresie matematică indisponibilă în copia arhivată], that meets the following conditions:
- The frequency of letter ‘[Expresie matematică indisponibilă în copia arhivată]‘ in [Expresie matematică indisponibilă în copia arhivată] is exactly [Expresie matematică indisponibilă în copia arhivată], the frequency of letter ‘[Expresie matematică indisponibilă în copia arhivată]‘ in [Expresie matematică indisponibilă în copia arhivată] is exactly [Expresie matematică indisponibilă în copia arhivată], and so on.
- Let’s assume characters of [Expresie matematică indisponibilă în copia arhivată] are numbered from [Expresie matematică indisponibilă în copia arhivată] to [Expresie matematică indisponibilă în copia arhivată], where [Expresie matematică indisponibilă în copia arhivată]. We apply the KMP algorithm to [Expresie matematică indisponibilă în copia arhivată] and get a table, [Expresie matematică indisponibilă în copia arhivată], of size [Expresie matematică indisponibilă în copia arhivată]. You must ensure that the sum of [Expresie matematică indisponibilă în copia arhivată] for all [Expresie matematică indisponibilă în copia arhivată] is minimal.
If there are multiple strings which fulfill the above conditions, print the lexicographically smallest one.
Input Format
A single line containing [Expresie matematică indisponibilă în copia arhivată] space-separated integers describing sequence [Expresie matematică indisponibilă în copia arhivată].
Constraints
- The sum of all [Expresie matematică indisponibilă în copia arhivată] will be a positive integer [Expresie matematică indisponibilă în copia arhivată].
Output Format
Print a single string denoting [Expresie matematică indisponibilă în copia arhivată].
Sample Input
2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Sample Output
aabb
Explanation
The output string must have two ‘[Expresie matematică indisponibilă în copia arhivată]‘ and two ‘[Expresie matematică indisponibilă în copia arhivată]‘. There are several such strings but we must ensure that sum of [Expresie matematică indisponibilă în copia arhivată] for all [Expresie matematică indisponibilă în copia arhivată] is minimal. See the figure below:

The minimum sum is [Expresie matematică indisponibilă în copia arhivată]. Among all the strings that satisfy both the condition, „aabb” is the lexicographically smallest.
Limbajul de programare folosit: cpp14
Cod:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned int uint;
typedef unsigned long long uint64;
typedef long long sint64;
uint v[26];
char s[1000005];
uint sn;
int main(int argc, char* argv[])
{
uint n = 26;
for (uint i = 0; i < n; ++i)
cin >> v[i];
uint mi = 0;
for (uint i = 0; i < 26; ++i)
{
if (v[i])
{
mi = i;
break;
}
}
uint mmi = mi;
for (uint i = mi + 1; i < 26; ++i)
{
if (v[i] && v[i] < v[mmi])
mmi = i;
}
s[sn++] = mmi + 'a';
--v[mmi];
if (mmi == mi)
{
for (uint i = mi + 1; i < 26; ++i)
{
if (v[i])
{
mmi = i;
break;
}
}
if (mi != mmi)
{
for (uint i = 0; i < v[mi]; ++i)
{
s[sn++] = mi + 'a';
s[sn++] = mmi + 'a';
--v[mmi];
}
v[mi] = 0;
}
}
for (uint j = 0; j < 26; ++j)
{
for (uint i = 0; i < v[j]; ++i)
s[sn++] = j + 'a';
}
cout << s << endl;
return 0;
}
Scor obtinut: 1.0
Submission ID: 464648885
Link challenge: https://www.hackerrank.com/challenges/kmp-problem/problem
