#include<bits/stdc++.h>
using
namespace
std;
int
* longestSubString(string X, string Y)
{
int
m = X.size();
int
n = Y.size();
int
result = 0;
int
endIndexX = 0;
int
endIndexY = 0;
int
cache[2][m+1];
int
currentRow = 0;
for
(
int
i = 0; i <= m; i++) {
for
(
int
j = 0; j <= n; j++) {
if
(i == 0 || j == 0) {
cache[currentRow][j] = 0;
}
else
if
(X[i-1] == Y[j-1]) {
cache[currentRow][j] = cache[1 - currentRow][j - 1] + 1;
if
(cache[currentRow][j] > result) {
result = cache[currentRow][j];
endIndexX = i - 1;
endIndexY = j - 1;
}
}
else
{
cache[currentRow][j] = 0;
}
}
currentRow = 1 - currentRow;
}
int
* x =
new
int
[3];
x[0] = (endIndexX - result + 1);
x[1] = (endIndexY - result + 1);
x[2] = result;
return
x;
}
string processString(string A,
int
index,
int
length)
{
string X = A.substr(0, index);
string Y =
""
;
for
(
int
i = 0; i < length; i++) {
Y +=
"0"
;
}
string Z = A.substr(index + length);
return
(X + Y + Z);
}
int
minimumSlice(string A,string B)
{
if
(A.size() < B.size())
return
-1;
if
(A == B)
return
0;
int
result = 0, n = (A.size() - 1);
while
(B.size() > 0) {
int
* processed = longestSubString(A, B);
if
(processed[2] == 0)
return
-1;
if
((processed[0] + processed[2] - 1 == n) || processed[0] == 0) {
if
(processed[0] == 0) {
if
(A[processed[0] + processed[2]] !=
'0'
)
result++;
}
else
{
if
(A[processed[0] - 1] !=
'0'
)
result++;
}
}
else
{
if
(A[processed[0] + processed[2]] !=
'0'
) {
result++;
}
if
(A[processed[0] - 1] !=
'0'
) {
result++;
}
}
B = B.substr(0, processed[1]) + B.substr(processed[1] + processed[2]);
A = processString(A, processed[0], processed[2]);
}
return
result;
}
int
main() {
cout << minimumSlice(
"topgames"
,
"mepo"
);
}