Вот простое решение с помощью итерации, в Java
.
Код
BuildString. java
public class BuildString {
/**
* Build a string.
*
* @param s the string to build,
* @param a point to append a char,
* @param b point to append a substring of existing part, b >= a,
* @return total point used,
*/
public static int build(String s, int a, int b) {
return build(s, a, b, new StringBuilder());
}
/**
* Build step by step, via iteration.
*
* @param s
* @param a
* @param b
* @param sb the built part,
* @return
*/
private static int build(String s, int a, int b, StringBuilder sb) {
int point = 0;
while (sb.length() < s.length()) {
String subStr = findMaxSubStr(sb, s);
if (subStr != null) {
sb.append(subStr);
point += b;
} else {
sb.append(s.charAt(sb.length()));
point += a;
}
}
return point;
}
/**
* Find next max substring with length >= 2.
*
* @param sb the existing part,
* @param s the original string.
* @return next max substring with length >= 2, or null if not exists,
*/
private static String findMaxSubStr(StringBuilder sb, String s) {
// TODO ... improve with cache ?
int sLen = s.length();
int sbLen = sb.length();
for (int i = sLen - sbLen; i > 1; i--) {
String target = s.substring(sbLen, sbLen + i);
if (sb.indexOf(target) >= 0) return target; // found,
}
return null; // not found,
}
}
BuildStringTest. java
(контрольный пример - через TestNG
)
import org.testng.Assert;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class BuildStringTest {
@Test
public void testBuild() {
testOnce("aabaacaba", 4, 5, 26);
testOnce("bacbacacb", 8, 9, 42);
}
@Test
public void testCorner() {
testOnce("", 4, 5, 0); // empty string,
testOnce("a", 4, 5, 4); // single char,
}
private void testOnce(String s, int a, int b, int expectedPoint) {
int point = BuildString.build(s, a, b);
System.out.printf("s = '%s', a = %d, b = %d, expected point: %d,\t actual point: %d\n", s, a, b, expectedPoint, point);
Assert.assertEquals(point, expectedPoint);
}
}
Сложность
- Время:
O(n^2)
- Пробел:
O(n)
TODO
- Я думаю, можно использовать какой-то кэш для ускорить поиск следующей максимальной подстроки, чтобы улучшить временную сложность.